0
我使用sails.js更新difference
變量的股票數據。當我這樣做,console.log(product.stock)
,值是4.但似乎product.save()
函數不會執行,因爲屬性stock
仍然沒有更改爲4.我猜這個問題是承諾。任何人都知道如何解決這個問題?SailsJS如何糾正這個諾言問題?
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;
return {
prevDetail: prevDetail,
difference: difference
};
})
.then(function(results) {
Product.findOne(results.prevDetail.product).then(function(product) {
product.stock += results.difference;
// maybe this below is not execute
product.save();
console.log(product.stock);
});
})
});
注意:我使用風帆0.10-rc8。已經用sails-mysql,sails-mongo,sails-postgres進行了測試,但仍然一樣。
爲什麼'.then()'? –