问题描述
我是续集的新手并试图减量。我有两种型号的产品和订单,在产品中有 productStock 列,在 order 中有数量列。我想要做的是,当我创建新订单时,productStock 将根据订购数量减少。代码如下:
router.post('/',async (req,res) => {
try {
const { productId,quantity } = req.body
const postData = new Order({
productId,quantity
})
const resultdata = await Product.findOne({ where: {productId: postData.productId} }).then( product => {
if(product.productStock <= 0) {
return res.json({
status: "Failed",message: "out of stock"
})
} else {
product.decrement('productStock',{by: postData.quantity})
return postData.save()
}
} ).catch(err => err)
res.send({
status: "success",data: resultdata
})
} catch (err) {
throw err
}
})
我得到了我想要的结果,但问题是当 productStock
(node:13236) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property '_writableState' -> object with constructor 'WritableState'
| property 'afterWriteTickInfo' -> object with constructor 'Object'
--- property 'stream' closes the circle
at JSON.stringify (<anonymous>)
at stringify (D:\Projects\sequelize-association\node_modules\express\lib\response.js:1123:12)
at ServerResponse.json (D:\Projects\sequelize-association\node_modules\express\lib\response.js:260:14)
at ServerResponse.send (D:\Projects\sequelize-association\node_modules\express\lib\response.js:158:21)
at D:\Projects\sequelize-association\routes\orders.js:30:13
at processticksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:13236) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated
either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:13236) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
请任何人帮忙,我也认为我的代码不干净,因为在 try/catch 中使用了 promise then/catch,所以如果我的情况有更好的解决方案,我将非常感谢....
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)