Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

javascript - 执行查询后Javascript变量被覆盖(Javascript variable being overwriiten after query execution)

I am using mysql database with Nodejs.(我正在使用带有Nodejs的mysql数据库。)

I am running a query like this:(我正在运行这样的查询:)
     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(`=====================================`)
            }

The reason I have so many console.log is because I have encountered a very weird bug.(我有这么多console.log的原因是因为我遇到了一个非常奇怪的错误。)

All of the values of rows[0].QuantityChange) should be the same.(rows[0].QuantityChange)所有值都应该相同。)

But my console shows:(但是我的控制台显示:)

====================================
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'

I know 425 is the value of ${id} variable which can be found at the end of the insert sql statement.(我知道425是${id}变量的值,可以在插入sql语句的末尾找到它。)

Which makes no sense to me.(这对我来说毫无意义。) What makes even less sense is how are the the bottom values are undefined or NAN(更没有意义的是底值如何未定义或NAN)   ask by jedu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not sure, but, in this part(我不确定,但是在这一部分)

'${(new Date ((new Date((new Date(new Date())).toISOString() )).getTime() - ((new Date()).getTimezoneOffset()*60000))).toISOString().slice(0, 19).replace('T', ' ')}'

Exist an incorrect use of single quotes, which can be altering the result of the consult.(存在不正确使用单引号的情况,这可能会更改咨询的结果。)

You should scape inner single quotes, or use Double quotes for wrapping the complete instruction.(您应该对内部单引号进行换码,或使用双引号将完整的指令包装起来。)
"${(new Date ((new Date((new Date(new Date())).toISOString() )).getTime() - ((new Date()).getTimezoneOffset()*60000))).toISOString().slice(0, 19).replace('T', ' ')}"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...