繁体   English   中英

使用Promise从Mongo DB检索对象时出错

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM