NodeJS验证并循环插入多个

时间:2018-09-04 21:22:41

标签: 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 + "')");
        });
      }
    });
  }
}

2 个答案:

答案 0 :(得分:0)

使用for loop代替使用map。在地图内部返回不含.then的每个查询。由于map返回一个新数组,因此您将获得一个promise数组。然后将该数组传递给Promise.all([yourArray])。在这种情况下,您将resolve进行所有查询。请停止使用var。切勿在{{1​​}}中使用它。不推荐使用我的Google样式指南。 https://google.github.io/styleguide/jsguide.html#features-local-variable-declarations

答案 1 :(得分:0)

遵循这些原则。

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
  });
}