node.js - How to synchronously call multiple functions which contain mongoose queries in nodejs? -
i want execute 4 functions in order. each functions of below format. create update delete , display. want call display function after completing other 3 functions. tried promise method without db call working in order.when mongoose call happening, order getting changed.
function createprojects(req) { var projects = req.body.created; projects.foreach(function (project) { var newproject = new projectschema(project); newproject.save(function (error, object) { if (error) { console.log(error) } console.log(object) }); }); } how issue can resolved?
you need code wait until 1 promise completes before starting next one, using bluebird.each, async library, or next callback queue. first part of sentence may sound duh sort of thing, in node.js .save return immediately, regardless of when callback executed (making less obvious in code).
here's example bluebird.each tried stick code style using in example code.
var promise = require('bluebird'); function createprojects(req) { var projects = req.body.created; return promise.each(projects, function (project) { return new projectschema(project).save(); }); }
Comments
Post a Comment