I'm learning nodejs, express and mongoose. I came up with a question regarding the findOne function used to retrieve a document from the database. Normally, you would use it like this:
 Product.findOne({_id: req.params.id},function(error, result){
      res.send(result);
});
But when I tried to do the following, it failed (I did it just for the sake of learning):
 Product.findOne({_id: req.params.id}, returnFunction(res));
 function returnFunction(res,error, result){ 
     //error and result  are provided by the findOne callback function
     return function(){
           res.send(result); //doesnt work
      };
  }
But if I pass the parameters to the inner function, it works perfectly:
Product.findOne({_id: req.params.id}, returnFunction(res));
 function returnFunction(res,error, result){ 
       //error and result  are provided by the findOne callback function
       return function(res,result){
             res.send(result); 
       };
 }
Isn't the inner function supposed to have access to the outer functions variable?
Thanks.
via Joel Alvarez Myrrie
 
No comments:
Post a Comment