Node.js:如何从回调中正确返回对象? [英] Node.js: How to properly return an object from within a callback?

查看:433
本文介绍了Node.js:如何从回调中正确返回对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在回调函数中定义/返回对象的正确方法是什么?



您可以在以下代码段中看到我的2个console.logs,回调之外的那个代码段当然是未定义的,我如何定义它?



在我的app.js中:

  var tools = require('../ models / tools.js'); 
app.get('/ games',requireAuth,function(req,res){
var gameqlist = tools.getMyGameQs(req,function(err,gameqlist){
console.log gameqlist); //这是正确定义的
return gameqlist; //不完全正确
});
console.log(gameqlist); //这是未定义
res。 render('games',{title:'Your Games!',gameqlist:gameqlist});


});

我有以下效用函数:


$ b b

tools.js:

  var Gameq = require('../ models / gameq'); 

module.exports = {
getMyGameQs:function(req,callback){
//查找用户所在的所有游戏队列
Gameq
.find({
'game.players.player_id':req.user.id
})
.asc('created_at')//按日期排序 - 获得最早的日期
.run(function(err,gameqlist){
if(!gameqlist){
err ='你不在任何游戏中';
}
return callback ,game_list);
});
}
};


解决方案

你不应该这样做。回调应该是异步的,因此在调用getMyGameQs之前的代码有可能在回调之前被执行。



你应该做什么调用res.render 从内部回调。

  var tools = require('../ models / tools.js'); 
app.get('/ game',requireAuth,function(req,res){
var gameqlist = tools.getMyGameQs(req,function(err,gameqlist){
res.render 'games',{title:'Your Games!',gameqlist:gameqlist});
});
});


Still wrapping my head around callbacks.

What's the proper way to define/return an object from within a callback function?

You can see my 2 console.logs in the following snippet, the one outside of the callback is of course undefined, how do I define it?

In my app.js:

  var tools = require('../models/tools.js');
  app.get('/games', requireAuth, function (req, res) {
    var gameqlist = tools.getMyGameQs(req, function(err, gameqlist){
      console.log(gameqlist); // this is properly defined
      return gameqlist; // not quite right
    });
    console.log(gameqlist); // this is undefined
    res.render('games', {title:'Your Games!', gameqlist : gameqlist});


  });

I have the following utility function which works fine:

tools.js:

var Gameq = require('../models/gameq');

module.exports = {
  getMyGameQs: function (req, callback){
    // find all game queues that a user is in
    Gameq
      .find({
                'game.players.player_id' : req.user.id
                })
      .asc('created_at') // sort by date - get oldest first
      .run(function(err, gameqlist) {
        if(!gameqlist){
          err = 'You are not in any games.';
        }
        return callback(err, gameqlist); 
      });
  }
};

解决方案

You shouldn't want to do that. Callbacks are supposed to be asynchronous, so there is a chance that the code after the call to getMyGameQs is executed before the callback.

What you should do call "res.render" from inside the callback.

var tools = require('../models/tools.js');
  app.get('/games', requireAuth, function (req, res) {
    var gameqlist = tools.getMyGameQs(req, function(err, gameqlist){
      res.render('games', {title:'Your Games!', gameqlist : gameqlist});
    });
  });

这篇关于Node.js:如何从回调中正确返回对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆