简体   繁体   English

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

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

相关问题 使用java在Mongo DB中插入数据,并使用javascript从Mongo DB中检索数据 - Inserting data in Mongo DB using java and retrieve the data from Mongo DB using javascript 使用 Promises 从 DynamoDB 异步检索数据 - Retrieve Data from DynamoDB asynchronously using Promises 使用承诺从 localStorage async 检索时出错 - Error when retrieving from localStorage async using promises 变量 scope 错误时尝试 JSON.stringify object 在存储所述 ZA8CFDE6331BD59EB2AC96F891ZB4 之后 - Variable scope error when attempting to JSON.stringify object after storing said object in Mongo DB 将字符串解析为对象以从Mongo DB查询 - Parse string to object to query from mongo db 对数据库和服务器使用Promise - Using promises for db and server 为什么使用 Promise 时没有保留 object state? - Why is object state not preserved when using Promises? 发布时如何在http语句中使用object_id引用另一个模型? (蒙戈数据库) - How to refer to another model using object_id in a http statement when posting? (mongo db) 尝试使用VueJS插值从嵌套对象的属性检索值时出错 - Error when trying to retrieve value from a property of a nested object using VueJS Interpolation 错误数组Ajax承诺使用“何时” - Error array Ajax promises using “when”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM