查询执行后Javascript变量被覆盖

标签 javascript node.js

我正在使用 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/

相关文章:

javascript - 如何让我的 jQuery 鼠标悬停工具提示跟随鼠标指针?

javascript - socket.io 客户端在长 Node.js 函数中自动断开连接

node.js - 使用 mongoose hook 重试保存重复键错误

javascript - 加载页面时Nodejs Web服务器两次调用requestListener

node.js - everyauth 密码示例错误

javascript - 如何从 Angular 模板(html 文件)调试对象

php - CKEditor:编辑模板(例如 PHP、JSP、ASP...即非标准 HTML)

javascript - 如何在 Laravel 应用程序上的 axios 中配置 header

javascript - 双向绑定(bind)无法识别 AngularJS 中的变量变化

node.js - Express JS - 检测计算机机器人、垃圾邮件