我的代码是插入重复的值在我的mysql表像下面的图像:我有一个map函数,它返回我处理过的数组值,如控制台中的以下数组:[[0,1,2英寸,含1 w的ENPG 6ViT 1“],[0,1,3英寸,含1 w的ENPG 6ViT 1”]]
如果我运行这个查询,它只插入2行在我的表中,这是正确的行为:
插入销售(标识销售额、数量、标识库存产品、标识销售额)值(0,1,2“,RWQt 9 x1e4 l”)、(0,1,3“,RWQt 9 x1e4 l”)
但我的代码插入了4行,如下图所示:
这是我的代码:
async function saleInsert(values) {
try {
let idSales = 0
let quantidade = -1
const teste = await values[0].map(obj => [idSales, 1, obj.idStock, idSale]);
console.log(JSON.stringify(teste))
const query = 'INSERT into sales (idSales, quantidade, idStockProduct, idSale) VALUES ?'
const result = await new Promise((resolve, reject) => {
conn.query(query, [teste], (error, results, fields) => {
if (error) return reject(error);
return resolve(results);
});
})
const updateTableStock = await new Promise((resolve, reject) => {
conn.query('update stock set quantidade = quantidade ? where idStock = ?', [quantidade, teste.idStock], (error, results, fields) => {
if (error) return reject(error);
return resolve(results);
})
})
console.log(`update stock table ${updateTableStock}`)
let tratado = JSON.stringify(result)
return tratado
} catch (err) {
console.log(err)
}
}
nodemon控制台似乎像这样复制插入:
[nodemon] starting `node ./bin/server.js`
app listening on port 8080
Connection established!
insert Sale
values insert sales [[{"idStock":2,"nome":"Produto 1","preco":2,"quantidade":-6},{"idStock":3,"nome":"produto 2","preco":1,"quantidade":10}]]
[[0,1,2,"ENPG6ViT1w"],[0,1,3,"ENPG6ViT1w"]]
update stock table [object Object]
insert Sale
values insert sales [[{"idStock":2,"nome":"Produto 1","preco":2,"quantidade":-6},{"idStock":3,"nome":"produto 2","preco":1,"quantidade":10}]]
[[0,1,2,"ENPG6ViT1w"],[0,1,3,"ENPG6ViT1w"]]
update stock table [object Object]
为什么这段代码在我的表中重复?任何想法都会有很大的帮助!!谢谢
1条答案
按热度按时间luaexgnf1#
你的saleInsert()函数看起来很好。你应该检查saleInsert()函数在哪里被调用,并确保它只被调用一次。
您还应该检查所使用的工具中是否存在导致双重插入的触发器