[英]Error when using promises to retrieve object from mongo db
Folks, I am trying to wrap the following with promises, and return a Mongo document. 伙计们,我试图用promise包装以下内容,并返回一个Mongo文档。
Controller's catch is getting error: 'TypeError: Cannot call method \\'apply\\' of undefined'
控制器的捕获
error: 'TypeError: Cannot call method \\'apply\\' of undefined'
What is the proper way to return the Object from the db to the controller? 将对象从数据库返回到控制器的正确方法是什么?
Controller: 控制器:
Q.fcall(Users.fetchId(authId)).then(function userVerification(userObject) {
console.log ('got back',userObject);
responses.Unauthorized('Unauthorized ID', res);
}).catch(function handleError(err) {
responses.InternalServerError(''+err, res);
});
Users Model: 用户模型:
Users.prototype.fetchId = function fetchId(authId) {
return this.mongodb.fetchId(authId);
};
Mongo: 蒙戈:
MongoDatabase.prototype.fetchId = function fetchId(id) {
var result = {}
return this.authDB.query('users', function(collection) {
return collection.findOne({_id:id}, function (err, doc) {
if (!_.isEmpty(doc)) {
var result = doc;
}
console.log ('mongo',result);
return result;
});
});
};
Q.fcall
takes a function as its first parameter, and then arguments to apply to that function. Q.fcall
将一个函数作为其第一个参数,然后将参数应用于该函数。 By passing it Users.fetchId(authId)
, you're passing it the result of calling that function. 通过将其传递给
Users.fetchId(authId)
,就将其传递给调用该函数的结果。
Try passing the function, and then the arguments you want to apply to it: 尝试传递函数,然后传递要应用到该函数的参数:
Q.fcall(Users.fetchId, authId).then( fn )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.