簡體   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