javascript - node.js functions returns undefined value - Stack Overflow

I have a problem getting the restaurantname from the db with node.js, it seems it has to do something w

I have a problem getting the restaurantname from the db with node.js, it seems it has to do something with callback parameters but I can’t find the solution for my case, hopefully one of you can help me .

I have made function who gets the name of the restaurant from a database. The first console log line gives the right retvalue and the second one gives undefined, how can I write the code so that the return value is the name of the restaurant?

Kind regards, Robert

function restaurantName(id) {
  var retvalue;
  try {
    F.model('restaurant').load(id).then(function (restaurant) {
    retvalue = restaurant.Name;
    console.log('restaurantName 1(' + id + ')' + retvalue);
    })
  } catch (err) {
    retvalue = 'Error';
  }
  console.log('restaurantName 2(' + id + ')' + retvalue);
  return retvalue;
};

I have a problem getting the restaurantname from the db with node.js, it seems it has to do something with callback parameters but I can’t find the solution for my case, hopefully one of you can help me .

I have made function who gets the name of the restaurant from a database. The first console log line gives the right retvalue and the second one gives undefined, how can I write the code so that the return value is the name of the restaurant?

Kind regards, Robert

function restaurantName(id) {
  var retvalue;
  try {
    F.model('restaurant').load(id).then(function (restaurant) {
    retvalue = restaurant.Name;
    console.log('restaurantName 1(' + id + ')' + retvalue);
    })
  } catch (err) {
    retvalue = 'Error';
  }
  console.log('restaurantName 2(' + id + ')' + retvalue);
  return retvalue;
};
Share Improve this question edited Feb 16, 2017 at 21:57 piotrbienias 7,4111 gold badge30 silver badges33 bronze badges asked Feb 16, 2017 at 15:12 user3493979user3493979 331 silver badge4 bronze badges 3
  • Can't you keep the second console.log inside the try block? – David R Commented Feb 16, 2017 at 15:27
  • You're not handling any error case in your callback function. – Matt D. Webb Commented Feb 16, 2017 at 15:32
  • 3 Possible duplicate of How do I return the response from an asynchronous call? – Ben Fortune Commented Feb 16, 2017 at 15:45
Add a ment  | 

1 Answer 1

Reset to default 6

Your function getting data from database is asynchronous, so the second console.log as well as return statement are done before the database operation finishes executing. You should return the value inside .then()

function restaurantName(id) {
    var retvalue;

    return F.model('restaurant').load(id).then(function (restaurant) {
        retvalue = restaurant.Name;
        console.log('restaurantName 1(' + id + ')' + retvalue);   
        return retvalue;           
    }).catch(function(error){
        return 'Error';
    });
};

And this function will also return a promise, so you would have to call it like that

restaurantName(1).then(function(result){
    console.log(result); // result would be retValue or 'Error'
});

EDIT

Your second problem, mentioned in a ment below this answer, is also concerned with how the promises work. In this case I remend you use Promise.all method which resolves when all of the promises passed as an argument will resolve

let promises = [];
snapshot.forEach(u => {
    let item = u.val();
    item.key = u.key;

    promises.push(restaurantName(item.restaurant).then(function(result){
        item.restaurantName = result;
        return item;
    }));
});

Promise.all(promises).then(result => {
    console.log(result); // here you get array of items 
});

What happens here is you create an array of promises that resolve with given value (which in this case is item object every time). When you pass this array to Promise.all method, it resolves to array of values that every single promise resolved to (if no error occurs of course)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744318042a4568278.html

相关推荐

  • javascript - node.js functions returns undefined value - Stack Overflow

    I have a problem getting the restaurantname from the db with node.js, it seems it has to do something w

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信