mongodb - Node.js callback unexpected behaviour with Mongoose -


i nodejs beginner , ran callback behaviour don't understand. using express router write mongoose objects mongodb using post request. in body of request passing in nested json structure 2 fields - jobdetails , examples. data in jobdetails used create job mongoose object , data in examples used create several example mongoose objects. job , example objects linked in job contains list of example objects in 1 of it's fields.

the way tried implement callbacks in following way. save job object mongo first, iterate on examples - each time creating example object , linking job via .job field , saving example object mongo. in callback example object save function updated job object new example object , saved updated version mongo.

router.post('/jobs', function (req, res, next) {     var job = new job(req.body.jobdetails);     var examples = req.body.examples;      console.log("job details");     console.log(req.body.jobdetails);      console.log("examples");     console.log(req.body.examples);      //save job     job.save(function (err, job) {             console.log(err);     });       //save examples      for(i=0; i<examples.length;i++){          var eg = new example({content: examples[i]});         eg.job=job;          eg.save(function (err, eg){              job.examples.push(eg);              job.save(function(err, job){                  console.log(err);              });             console.log(err);         });     } }); 

this did not perform expect. specifically, double number of examples saved mongo several duplicates , missing. understand callbacks asynchronous me still doesn't explain why double number of examples saved , duplicated , missing.

i got work correctly without using callbacks @ in following way.

router.post('/jobs', function (req, res, next) {     var job = new job(req.body.jobdetails);     var examples = req.body.examples;      console.log("job details");     console.log(req.body.jobdetails);      console.log("examples");     console.log(req.body.examples);      //save job     job.save(function (err, job) {           console.log(err);     });      //save examples     for(i=0; i<examples.length;i++){          var eg = new example({content: examples[i]});         eg.job=job;          eg.save(function (err, eg){             console.log(err);         });          job.examples.push(eg);         job.save(function(err,job){             console.log(err);         });     } }); 

and i'm not sure if optimal solution either. know why original approach resulted in unintended behaviour.

this should work..

router.post('/jobs', function(req, res, next) {     var job = new job(req.body.jobdetails);     var examples = req.body.examples;      console.log("job details");     console.log(req.body.jobdetails);      console.log("examples");     console.log(req.body.examples);      //save job     job.save(function(err, result) {         if (!err) {             //save examples             (i = 0; < examples.length; i++) {                  var eg = new example({                     content: examples[i]                 });                 eg.job = job;                  eg.save(function(err, eg) {                      job.examples.push(eg);                      job.save(function(err, job) {                         if (!err)                             job.examples = [];                      });                     console.log(err);                 });             }         }     }); }); 

Comments