Все еще оборачиваясь вокруг обратных вызовов.
Как правильно определить / вернуть объект из функции обратного вызова?
Вы можете увидеть мои 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); // this is properly defined
return gameqlist; // not quite right
});
console.log(gameqlist); // this is undefined
res.render('games', {title:'Your Games!', gameqlist : gameqlist});
});
У меня есть следующая служебная функция, которая прекрасно работает:
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);
});
}
};