1

Folks, I am trying to wrap the following with promises, and return a Mongo document.

Controller's catch is getting 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;
        });
    });
};

1 Answer 1

2

Q.fcall takes a function as its first parameter, and then arguments to apply to that function. By passing it Users.fetchId(authId), you're passing it the result of calling that function.

Try passing the function, and then the arguments you want to apply to it:

Q.fcall(Users.fetchId, authId).then( fn )
Sign up to request clarification or add additional context in comments.

1 Comment

hmmm, now mongo never gets called. The Users Model errors out with { error: 'TypeError: Cannot read property \'mongodb\' of undefined' }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.