Node, Mongoose - Can't access parent scope in query callback

断了今生、忘了曾经 提交于 2019-12-12 05:50:40

问题


I'm a bit confused on why I am not seeing the parent scope inside a Mongoose Query Callback, here is my code:

var P = require('bluebird');

var getUser = P.promisify(
    function(userId, locals, next){

        logger.info(locals); //{offer: 1}

        User.findOne({_id: userId}).lean().exec(function(err, user){
            logger.info(locals); //undefined
            if(err){return next(err);}
            if(!locals)
                locals = {user: user};
            else{
                locals.user = user;
            }
            return next(null, locals);
        });
    }
);

var locals = {offer: 1};

getUser(user._id, locals);

I'm calling the getUser function with two params, in the function's scope I can see them both, but I need to access the locals variable inside the mongoose callback and not seeing the parent scope value in that scope.

Any idea on how to solve this?

Thanks in advance

来源:https://stackoverflow.com/questions/26969579/node-mongoose-cant-access-parent-scope-in-query-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!