Hotline Logo freshchat

Start a new topic

$this.db.get store as variable for global use

Hi Support,


May I know can $this.db.get can pass the result into and stored as variable for global variable? In the following, the "return data.aws_ticket_id" has indeed stored the id value, but I have struggling to find out the way to use it in global context


  GetStoredData : function(f_ticket_id) {   

       this.$db.get("ticket: " + f_ticket_id + "").done(function(data) {
         return data.aws_ticket_id;       
       })   
       .fail(function(err) {
         return err;
       });
     }


I have tried first declare in global context:

var f_ticket_id = domHelper.ticket.getTicketInfo().helpdesk_ticket.display_id;


then

console.log(self_GetStoredData(f_ticket)); --> return "undefined"


Appreciate your help!


Keen

1 Comment

All of this.$db operations are asynchronous. In case of asynchronous operations, values cannot be returned. So callbacks must be used to pass the values.


Example:

GetStoredData : function(f_ticket_id, callback) {   

    this.$db.get("ticket: " + f_ticket_id + "").done(function(data) {

      callback(null, data.aws_ticket_id);       

    })   

    .fail(function(err) {

      callback(err);

    });

}


Calling the function

GetStoredData(f_ticket_id, function(err, data) {

    //err & data

});


Login or Signup to post a comment