[英]KnexJS Transaction
如何執行該交易? 我的問題是正確使用for
指令。
app.db.transaction(function (trx) {
app.db('sales_record_products').transacting(trx).insert(products)
.then(_ => {
for (let i = 0; i < stockProducts.ids.length; i++) {
app.db('products').transacting(trx).where('id',stockProducts.ids[i]).update({quantity: stockProducts.results[i]})
.then(_ => {
})
}
})
.then(trx.commit)
.catch(trx.rollback)
})
.then(_ => res.status(200).json({success: true}))
.catch(err => res.status(500).send(err))
為什么不使用類似的東西
// Start a database transaction
return app.db.transaction(function (trx) {
// Insert products
return trx.insert(products)
.then(function (newIds) {
// Product inserted, you have the new database id in newIds
// Update all product individually but sequentially
return Promise.mapSeries(stockProducts, function (sp) {
// Update a single product
return trx
.table('products')
.where('id', stockProducts.ids[i])
.update({ quantity: stockProducts.results[i] });
});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.