我有这个函数可以从 mongo db 中检索特定用户:
exports.findById = function(req, res){
var id = req.params.id;
db.collection('Users', function(err, collection){
collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item){
findPhotoByUserId(item._id, function(photo){
console.log('photo: ' + photo);
});
});
});
};
而另一个获取用户的照片,将用户的 ID 传递给它:
var findPhotoByUserId= function(userId, cb){
db.collection('Photos', function(err, collection){
collection.findOne({'userId': userId}, function(err, item){
if(!err)
cb(item);
});
});
};
问题:当我调用函数“findById”时,照片项为空。但是,如果我将显式用户 ID 放在这里“collection.findOne({'userId': '522bae4a3ee1be8005000001'}....”,该函数将返回预期的照片项。
有人可以帮我解决这个问题吗?
谢谢!