express.js不等待循环

时间:2018-08-15 13:42:03

标签: javascript express

我想让用户详细说明json,但是我的代码不等待循环 并打印{“ personalchats”:[]}

我想在for循环后发送

我该如何解决此问题?

connection.query("SELECT * FROM personalchat WHERE user1ID = ? OR user2ID = ?", [userID, userID], function(err, row, fields) {
  if (err)
    console.log(err);

  else {
    personalchats = Array();

    for (var i = 0; i < row.length; i++) {
      if (row[i].user1ID != userID)
        user2ID = row[i].user1ID;
      else
        user2ID = row[i].user2ID;

      connection.query("SELECT * FROM users WHERE userID = ?", [user2ID], function(err2, row2, fields2) {
        if (err)
          console.log(err2);

        else {


          personalchats.push({
            'success': true,
            'userID': row2[0].userID,
            'name': row2[0].name,
            'surname': row2[0].surname,
            'email': row2[0].email
          });
          console.log(personalchats);
        }


      });

    }

    res.send({
      "personalchats": personalchats
    });
  }

});

3 个答案:

答案 0 :(得分:0)

您遇到了异步问题。您对数据库的调用需要花费一些时间才能完成,并且很有可能在尝试发送数据之前您不会收到响应并运行回调。在这种情况下,使用'Promise.all'可能是个好主意,或者构建某种类型的函数,该函数在将每个项目推入回调内的数组后运行,该函数将检查发送之前是否已收到所有响应表达回应。

答案 1 :(得分:0)

您的代码是异步的。首先connection.query通过调用回调function(err, row, fields) { }返回结果。

第二个查询还通过调用回调函数function(err2, row2, fields2) { }返回。您可以从该回调函数中读取值。您可以登录为console.log(personalchats)。另外,您还必须在该回调函数中发送结果。

  personalchats.push({
    'success': true,
    'userID': row2[0].userID,
    'name': row2[0].name,
    'surname': row2[0].surname,
    'email': row2[0].email
  });
  console.log(personalchats);
  res.send({
    "personalchats": personalchats
  });

否则row2的值将在回调之外不可用。

您应在connection.query成功之后发送结果。完整示例:

connection.query("SELECT * FROM personalchat WHERE user1ID = ? OR user2ID = ?", [userID, userID], function(err, row, fields) {
  if (err)
    console.log(err);

  else {
    personalchats = Array();

    for (var i = 0; i < row.length; i++) {
      if (row[i].user1ID != userID)
        user2ID = row[i].user1ID;
      else
        user2ID = row[i].user2ID;

      connection.query("SELECT * FROM users WHERE userID = ?", [user2ID], function(err2, row2, fields2) {
        if (err)
          console.log(err2);

        else {


          personalchats.push({
            'success': true,
            'userID': row2[0].userID,
            'name': row2[0].name,
            'surname': row2[0].surname,
            'email': row2[0].email
          });
          console.log(personalchats);
          res.send({
            "personalchats": personalchats
          });
        }


      });

    }


  }

});

答案 2 :(得分:0)

使用此代码:

connection.query("SELECT * FROM personalchat WHERE user1ID = ? OR user2ID = ?", [userID, userID], function(err, row, fields) {
  if (err)
    console.log(err);

  else {
      personalchats = [];
      var userIDs = [];

      for (var i = 0; i < row.length; i++) {
        if (row[i].user1ID != userID)
          userIDs.push(row[i].user1ID);
        else
          userIDs.push(row[i].user2ID);
      }

      connection.query("SELECT * FROM users WHERE userID in (" + userIDs.join(',') + ")", function(err2, row2, fields2) {
        if (err)
          console.log(err2);

        else {

          row2.forEach(r =>
            personalchats.push({
              'success': true,
              'userID': r.userID,
              'name': r.name,
              'surname': r.surname,
              'email': r.email
            });
          })
          console.log(personalchats);
          res.send({
            "personalchats": personalchats
          });
        }
      });
    }
  }
});