gpt4 book ai didi

mysql - 循环查询在 Node js 中 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:33:19 24 4
gpt4 key购买 nike

我有一个循环,我正在检查记录是否存在,如果没有插入,问题是我的控制台显示循环内的代码没有逐行运行,它首先运行所有选择,然后运行所有插入
从excel循环

tablesdata.map(async function (table) {   
check if record exist
await product_meta
.count({
where: [
{
title: fr_name,
language_id: 2,
},
],
})
.then((count) => {
console.log(count);
if (count > 0) {
console.log(count);
} else {
insert record create_pr = products.create(product, {});product_id = create_pr.id;
}

)}
)}

最佳答案

使用 async/await 会更直接 - Array.map() 返回一个可以使用 Promise.all() 解析的 promise 数组。

const promises = tablesdata.map(async function (table) {
if (recordExists) {
const count = await product_meta.count({
where: {
title: fr_name,
language_id: 2,
},
});

if (!count) {
// this will create a new product, perhaps it is meant to be a product meta?
const create_pr = await products.create(product, {});
// return the created ID
return create_pr.id;
}

console.log("there are already", count, "product metas");
return null;
}
});

// this will resolve an array of newly created IDs,
const allResults = await Promise.all(promises);
// filter out the null entries
const productIds = allResults.filter((result) => result);

关于mysql - 循环查询在 Node js 中 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64747745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com