Node, Mongoose - Can't access parent scope in

2019-09-03 11:28发布

问题:

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