0

Hi I have the following code but temp always returns undefined and I have tried passing variables through as parameters but haven't found a solution. How could I get the value of temp to return the correct value. I know that in javascript the order is unimportant however I dont know how to fix that. Thanks

function get_account (id){
  temp = "";
  Account.get(id, function (err, acc) {
    temp = acc.get("Account");

 });
 return temp
}
Arthur Le Calvez
  • 413
  • 2
  • 7
  • 17
  • I bet `get_account` *actually* returns an empty string (`""`) ... not `undefined` - also, global variables suck :p – Jaromanda X Jan 23 '18 at 05:17
  • 1
    Please check this block, What is happening in Account.get(id, function (err, acc) { temp = acc.get("Account"); }); what is happening in Account.get function..? – Vinod kumar G Jan 23 '18 at 05:18
  • If i do console.log(acc.get("Account")) it prints the correct value as a string which is what i expecected – Arthur Le Calvez Jan 23 '18 at 05:20
  • you need to use `promise` to return the correct value – Riad Jan 23 '18 at 05:27
  • 1
    Try this ```function get_account(id, callback){ Account.get(id,function(err, acc){ if(err){ return callback(err); }else{ return callback(acc.get(“Account”)) } }) } get_account(id,function(err, callback){ console.log(callback); });``` – Santosh Suryawanshi Jan 23 '18 at 16:46

0 Answers0