简体   繁体   English

node.js函数返回未定义的值

[英]node.js functions returns undefined value

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 . 我在使用node.js从数据库获取餐厅名称时遇到问题,似乎它必须使用回调参数来做一些事情,但是我找不到适合我情况的解决方案,希望你们中的一个可以帮助我。

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? 第一个控制台日志行提供正确的retvalue,第二个控制台日志给出未定义的值,如何编写代码,使返回值成为餐厅的名称?

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;
};

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. 从数据库获取数据的函数是异步的,因此第二个console.log以及return语句是在数据库操作完成执行之前完成的。 You should return the value inside .then() 您应该在.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 而且此函数还将返回一个promise ,因此您必须像这样调用它

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

EDIT 编辑

Your second problem, mentioned in a comment below this answer, is also concerned with how the promises work. 在此答案下方的评论中提到的第二个问题,也是关于诺言如何工作的问题。 In this case I recommend you use Promise.all method which resolves when all of the promises passed as an argument will resolve 在这种情况下,我建议您使用Promise.all方法来解析何时将所有作为参数传递的promise解析

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). 在这里发生的是您创建了一个以给定值解析的promise数组(在这种情况下,每次都是item对象)。 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) 当将此数组传递给Promise.all方法时,它将解析为每个单个promise解析为的值的数组(当然,如果没有错误发生)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM