node.js - Why can't you modify the data returned by a Mongoose Query (ex: findById) -
when try change part of data returned mongoose query has no effect.
i trying figure out 2 hours yesterday, kinds of _.clone()s, using temporary storage variables, etc. finally, when though going crazy, found solution. figured in future (fyuuuture!) might have save issue.
survey.findbyid(req.params.id, function(err, data){     var len = data.survey_questions.length;     var counter = 0;      _.each(data.survey_questions, function(sq){         question.findbyid(sq.question, function(err, q){             sq.question = q; //has no effect              if(++counter == len) {                 res.send(data);             }         });     }); });      
for cases want plain js object instead of full model instance, can call lean() on query chain so:
survey.findbyid(req.params.id).lean().exec(function(err, data){     var len = data.survey_questions.length;     var counter = 0;      _.each(data.survey_questions, function(sq){         question.findbyid(sq.question, function(err, q){             sq.question = q;              if(++counter == len) {                 res.send(data);             }         });     }); });   this way data plain js object can manipulate need to.
Comments
Post a Comment