我在验证和向数据库中插入多个条目时遇到问题。
nodejs查询在for循环中,但查询方法是异步执行的。并非参数“snapshot”中的所有条目都插入到数据库中。我怎样才能解决这个问题?
这里我设计了一个流程图来向你展示我的逻辑。
我非常感谢你的帮助。
提前谢谢。
这是我的密码:
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 + "')");
});
}
});
}
}
2条答案
按热度按时间f0ofjuux1#
沿着这条线。
j2qf4p5b2#
而不是使用
for loop
使用map
. 在Map内部返回每个查询.then
. 因为map
返回一个新数组,您将得到一个承诺数组。然后把那个数组传给Promise.all([yourArray])
. 那样的话你会的resolve
你所有的疑问。请停止使用var
一般来说。千万不要用它来做手术for loop
. 这是不鼓励我的谷歌风格的指南。https://google.github.io/styleguide/jsguide.html#features-局部变量声明