sails.js - SailsJS how to correct this promise issue? -
sails.js - SailsJS how to correct this promise issue? -
i utilize sails.js update stock info difference
variable. when do, console.log(product.stock)
, value 4. seems product.save()
function below not executed because attribute stock
still not changed 4. guess problem @ promise. know how prepare this?
exports.updateproductstock = function (details) { details.foreach(function( detail ) { var findprevdetailrule = { invoice: detail.invoice, product: detail.product.id }; var createprevdetailrule = { invoice: detail.invoice, product: detail.product.id, quantity: detail.quantity }; var requirementbeforeupdatestock = [ previousdetail.findone(findprevdetailrule).sort('createdat desc').then(), previousdetail.create(createprevdetailrule).then() ]; q.all(requirementbeforeupdatestock) .spread(function( prevdetail, newprevdetail ) { var difference = detail.quantity - prevdetail.quantity; homecoming { prevdetail: prevdetail, difference: difference }; }) .then(function( results ) { product.findone(results.prevdetail.product).then(function(product) { product.stock += results.difference; // maybe below not execute product.save(); console.log(product.stock); }); }) });
note: utilize sails 0.10-rc8. have tested sails-mysql, sails-mongo, sails-postgres, still same.
.save()
accepts callback can study success/failure.
for example, can repost client (from sails documentation) :
product.save(function (err) { if (err) homecoming res.send(err, 500); res.json(product); });
depending on scope, may need pass res
function
alternatively, monitor success/failure in console, eg ,
product.save(function (err) { if (err) console.log('save error: ' + err);//assuming err string console.log('save success'); });
should @ to the lowest degree give clue what's going wrong.
promise sails.js waterline
Comments
Post a Comment