我在验证多个条目并将其插入数据库时遇到问题。
NodeJS 查询位于 for 循环内,但查询方法是异步执行的。并非参数“快照”中的所有条目都会插入数据库中。我该如何解决这个问题?
非常感谢您的帮助。
提前致谢。
这是我的代码:
function setAddListener(userId, snapshot) {
//var item = snapshot.val();
//var key = Object.keys(snapshot.val())[0];
//var size = Object.keys(snapshot.val()).length;
for (var i = 0; i < Object.keys(snapshot.val()).length; i++) {
var item = Object.values(snapshot.val())[i];
//console.log(item);
console.log(item.shoppingListName);
console.log(item.userId);
console.log(item.shoppingListsKey);
console.log(item.shoppingListKey);
console.log("QUERY: " + "SELECT * FROM bc_shopping_list WHERE shopping_list_id = '" + item.shoppingListKey + "'");
databasemysql.query("SELECT * FROM bc_shopping_list WHERE shopping_list_id = '" + item.shoppingListKey + "'").then(rows => {
//console.log(results.length);
if (error) throw error;
if (results.length == 1) {
console.log("Shopping-List exists :" + item.shoppingListName);
} else {
console.log("Not exits");
//console.log("INSERT INTO `bc_shopping_list`(`shopping_list_id`, `user_id_fk`, `article_user_id_fk`, `shopping_list_picture_id`, `article_search`, `shopping_list_name`, `shopping_list_quantity`, `unit_id_fk`, `shopping_list_durability`, `shopping_list_bought`, `place_id_fk`, `firebase_id`) VALUES ('" + item.shoppingListKey + "', '" + item.userId + "', " + item.articleId + ", '', NULL,'" + item.shoppingListName + "', " + item.shoppingListQuantity + ", " + item.unitId+1 + ", '" + item.shoppingListDurability + "', " + item.shoppingListBought + ", " + item.placeId + ", '" + item.shoppingListsKey);
//connection.query("INSERT INTO `bc_shopping_list`(`shopping_lists_name`, `user_id_fk`, `shopping_lists_bought`, `firebase_id`) VALUES ('" + dataSnapshot.val().shoppingListsName + "','" + dataSnapshot.val().userId + "', " + dataSnapshot.val().shoppingListsBought + ", '" + postId2 + "')", function (error, results, fields) {
//console.log("INSERT INTO `bc_shopping_list`(`shopping_list_id`, `user_id_fk`, `article_user_id_fk`, `shopping_list_picture_id`, `article_search`, `shopping_list_name`, `shopping_list_quantity`, `unit_id_fk`, `shopping_list_durability`, `shopping_list_bought`, `place_id_fk`, `firebase_id`) VALUES ('" + item.shoppingListKey + "', '" + item.userId + "', " + item.articleId + ", '', NULL,'" + item.shoppingListName + "', " + item.shoppingListQuantity + ", " + item.unitId+1 + ", '" + item.shoppingListDurability + "', " + item.shoppingListBought + ", " + item.placeId + ", '" + item.shoppingListsKey + "')");
connection.query("INSERT INTO `bc_shopping_list`(`shopping_list_id`, `user_id_fk`, `article_user_id_fk`, `shopping_list_picture_id`, `article_search`, `shopping_list_name`, `shopping_list_quantity`, `unit_id_fk`, `shopping_list_durability`, `shopping_list_bought`, `place_id_fk`, `firebase_id`) VALUES ('" + item.shoppingListKey + "', '" + item.userId + "', " + item.articleId + ", '', NULL,'" + item.shoppingListName + "', " + item.shoppingListQuantity + ", " + item.unitId + 1 + ", '" + item.shoppingListDurability + "', " + item.shoppingListBought + ", " + item.placeId + ", '" + item.shoppingListsKey + "')", function(error, results, fields) {
console.log("Shopping-Lists Added :" + item.shoppingListName);
//console.log("INSERT INTO `bc_shopping_list`(`shopping_list_id`, `user_id_fk`, `article_user_id_fk`, `shopping_list_picture_id`, `article_search`, `shopping_list_name`, `shopping_list_quantity`, `unit_id_fk`, `shopping_list_durability`, `shopping_list_bought`, `place_id_fk`, `firebase_id`) VALUES ('" + item.shoppingListKey + "', '" + item.userId + "', " + item.articleId + ", '', NULL,'" + item.shoppingListName + "', " + item.shoppingListQuantity + ", " + item.unitId+1 + ", '" + item.shoppingListDurability + "', " + item.shoppingListBought + ", " + item.placeId + ", '" + item.shoppingListsKey + "')");
});
}
});
}
}
最佳答案
大致上是这样的。
function setAddListener(userId, snapshot) {
const keys = Object.keys(snapshot.val());
const results = keys.map((item) => {
return databasemysql.query("SELECT * FROM bc_shopping_list WHERE shopping_list_id = '" + item.shoppingListKey + "'")
});
Promise.all(results).then((data) => {
// data is an array of resolved queries.
// do your operations here
});
}
关于javascript - NodeJS 验证和循环中的多次插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174150/