![](/img/trans.png)
[英]Inserting data in Mongo DB using java and retrieve the data from Mongo DB using javascript
[英]Error when using promises to retrieve object from mongo db
伙计们,我试图用promise包装以下内容,并返回一个Mongo文档。
控制器的捕获error: 'TypeError: Cannot call method \\'apply\\' of undefined'
将对象从数据库返回到控制器的正确方法是什么?
控制器:
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.prototype.fetchId = function fetchId(authId) {
return this.mongodb.fetchId(authId);
};
蒙戈:
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
将一个函数作为其第一个参数,然后将参数应用于该函数。 通过将其传递给Users.fetchId(authId)
,就将其传递给调用该函数的结果。
尝试传递函数,然后传递要应用到该函数的参数:
Q.fcall(Users.fetchId, authId).then( fn )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.