javascript - NodeJS 验证和循环中的多次插入

标签 javascript mysql node.js

我在验证多个条目并将其插入数据库时​​遇到问题。
NodeJS 查询位于 for 循环内,但查询方法是异步执行的。并非参数“快照”中的所有条目都会插入数据库中。我该如何解决这个问题?

这里我设计了一个流程图来展示我的逻辑。 enter image description here

非常感谢您的帮助。

提前致谢。

这是我的代码:

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/

相关文章:

php - 阻止MySQL用空字符串更新列?

javascript - ng-change 不适用于我的半自定义日历控件

php - prev() 和 next() 不返回结果

php - 使用 php 循环 json 数组

PHP 和 SQL : Can the same statement return different resultsets by the fact whether it where send by a php script or was send directly via a . SQL 文件?

node.js - sinon 不是模拟导入函数

node.js - Passport.authenticate() 使用 Promise 而不是自定义回调

node.js - 在 MacOS 中使用 Homebrew 安装 Node 时卡住:fork.rb:49:in `write' : 管道损坏 (Errno::EPIPE)

javascript - 如何更改数组字符串中特定单词的颜色? : Javascript/jQuery

javascript - D3 力布局更改显示与 slider 的链接