I am using now.js and Mongoose in a node project and am having trouble accessing the this.now object inside of a mongoose function. E.g.
everyone.now.joinDoc = function (project_id){
this.now.talk(); //this will work
Project.findOne({'_id':project_id}, function(err, project){
if(project){
this.now.talk(); // this will not work "TypeError: Cannot call method 'bark' of undefined"
};
});
};
Change the code to this:
Inside your inner function, the
thisis probably being set to something else. So, to preserve the value you want to access, you assign it to a different local variable that will be available in the inner function.