[英]KnexJS Transaction
How can I execute this transaction? 如何执行该交易? My problem is the correct use of for
instruction. 我的问题是正确使用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))
Why not using something like 为什么不使用类似的东西
// 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.