我正在使用 mysql 数据库和 Nodejs。我正在运行这样的查询:
rows = await pool.query(`Select partID,stageID,locationID,QuantityAfter, QuantityChange, rowDeleted from InventoryTransactions
where partID = ${item.partID} and stageID = ${item.stageID} and locationID = ${item.locationID} and orderID = ${orderID}
order by transactionID desc`)
if(rows[0].rowDeleted != 1){ //This means that the quantity in the inventory table and inv trans table is not the same
console.log(`=====================================`)
console.log(Number(rows[0].QuantityChange))
console.log(Number(rows[0].QuantityChange))
console.log(Number(rows[0].QuantityChange))
await pool.query(`Insert into inventorytransactions(OrderID,PartID,LocationID,StageID,QuantityBefore,QuantityChange,QuantityAfter,ITTTimeStamp,rowDeleted,id)
Values(${orderID},${rows[0].partID},${rows[0].locationID},${rows[0].stageID},${parseInt(rows[0].QuantityAfter)},${-parseInt(rows[0].QuantityChange)},${parseInt(rows[0].QuantityAfter)-parseInt(rows[0].QuantityChange)},'${(new Date ((new Date((new Date(new Date())).toISOString() )).getTime() - ((new Date()).getTimezoneOffset()*60000))).toISOString().slice(0, 19).replace('T', ' ')}',true,${id})`)
console.log(Number(rows[0].QuantityChange))
console.log(Number(rows[0].QuantityChange))
console.log(Number(rows[0].QuantityChange))
console.log(`Update inventory set Quantity = quantity - ${Number(rows[0].QuantityChange)} where partID =${rows[0].partID} and stageID = ${rows[0].stageID} and locationID = ${rows[0].locationID}`)
await pool.query(`Update inventory set Quantity = quantity - ${Number(rows[0].QuantityChange)} where partID =${rows[0].partID} and stageID = ${rows[0].stageID} and locationID = ${rows[0].locationID} `)
console.log(`=====================================`)
}
我之所以有这么多console.log
是因为我遇到了一个非常奇怪的bug。 rows[0].QuantityChange)
的所有值都应该相同。
但是我的控制台显示:
====================================
4
4
4
425
NaN
NaN
NaN
Update inventory set Quantity = quantity - NaN where partID =12462 and stageID = 1 and locationID = 1
{ Error: ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'field list'
我知道425是${id}
变量的值,可以在insert sql语句的末尾找到。这对我来说毫无意义。更没有意义的是底部值是如何未定义的或 NAN
最佳答案
425
可能来自您的 await pool.query()
语句期间运行的一些其他代码(而不是您向我们展示的代码中的任何内容)。 await
会阻止本地函数的执行,但它不会阻止事件队列,因此在 await ...
等待完成时其他事件可以运行。
然后,您没有显示 rows
的定义位置,但它似乎可能被 await
期间运行的某些其他代码覆盖。如果您向我们展示有关此代码的更多上下文,包括定义 rows
的位置,我们可以提供更详细的建议和解释。
关于查询执行后Javascript变量被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59113021/