如何在 for 循环中处理回调 (Node.JS)

How to handle callbacks in a for loop(Node.JS)

我正在尝试使用 NodeJS 编写代码,我从外部 API 获取数据,然后使用 Mongoose 将它们填充到 MongoDB 中。在此期间,我将检查该特定内容是否已存在于 Mongo 中。下面是我的代码。

router.route('/report') // the REST api address
  .post(function(req, res) // calling a POST 
  {
    console.log('calling report API');
    var object = "report/" + reportID; // related to the API
    var parameters = '&limit=100' // related to the API
    var url = link + object + apiKey + parameters; // related to the API

    var data = "";
    https.get(url, function callback(response)
    {
      response.setEncoding("utf8");
      response.on("data", function(chunk)
      {
        data += chunk.toString() + ""; 
      });

      response.on("end", function()
      {
        var jsonData = JSON.parse(data);
        var array = jsonData['results']; // data is return in array of objects. accessing only a particular array
        var length = array.length;
        console.log(length);

        for (var i = 0; i < length; i++) 
        {
          var report = new Report(array.pop()); // Report is the schema model defined. 
          console.log('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^');
          console.log(i);
          console.log('*****************************');
          console.log(report);
          console.log('*****************************');
          // console.log(report['id']);

          /*report.save(function(err)
          {
            if(err)
              res.send(err);
          });*/

          Report.find({id:report['id']}).count(function(err, count) // checks if the id of that specific data already exists in Mongo
          {
            console.log(count);
            console.log('*****************************');
            if (count == 0) // if the count = 0, meaning not exist, then only save
            {
              report.save(function(err)
              {
                console.log('saved');
                if(err)
                  res.send(err);
              });
            }
          });
        };
        res.json({
                    message: 'Grabbed Report'
                  }); 
      });
      response.on("error", console.error);
    });
  })

我的问题是,由于 NodeJS 回调是并行的,因此不会按顺序调用。我的最终结果是这样的:

  1. 来电报告API
  2. console.log(长度)=100
  3. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  4. console.log(i) = 以 0
  5. 开头
  6. *********************************
  7. console.log(report) = 将存储在Mongo
  8. 中的数据
  9. *********************************
  10. 数字 3 - 7 重复 100 次,因为长度等于 100
  11. console.log(计数)= 0 或 1
  12. 数字 9 重复 100 次
  13. console.log('saved')
  14. 数字 11 重复 100 次
  15. 最后,只有 100 个数据中的最后一个被存储到 Mongo

我需要的是某种技术或方法来处理这些回调,这些回调一个接一个地执行,而不是按顺序执行循环。我很确定这是问题所在,因为我的其他 REST API 都在工作。

我研究了异步方法、promises、递归函数和其他一些我无法真正理解如何解决此问题的方法。我真的希望有人能对这件事有所了解。

如果我在提问的方式上有任何错误,请随时纠正我。这是我在 Whosebug 中发布的第一个问题。

您可以使用 async 库来控制您的执行流程。还有用于处理数组的迭代器。

这个问题被称为"callback hell"。 您会发现许多其他方法,例如使用 Promise and Async 库。

我更期待原生 async ES7 会带来, 实际上,您今天就可以开始使用转译器库 Babel.

但到目前为止,我发现的最简单的方法如下: 您取出长回调函数并在外部定义它们。

router.route('/report') // the REST api address
    .post(calling_a_POST)

function calling_a_POST(req, res) {
    ...
    var data = "";
    https.get(url, function callback(response) {
        ...
        response.on("end", response_on_end_callback); // --> take out
        response.on("error", console.error);
    });
}

function response_on_end_callback() {                 // <-- define here
    ...
    for (var i = 0; i < length; i++) {
        var report = new Report(array.pop());
        ...
        Report.find({ id: report['id'] })
              .count(Report_find_count_callback);     // --> take out
    };
    res.json({
        message: 'Grabbed Report'
    });
}

function Report_find_count_callback(err, count) {     // <-- define here
    ...
    if (count == 0) {
        report.save(function(err) {                   // !! report is undefined here
            console.log('saved');
            if (err)
                res.send(err);                        // !! res is undefined here
        });
    }
}

需要注意的是,您将无法访问曾经是回调的所有变量, 因为您已将它们排除在范围之外。

这可以通过 "dependency injection" 包装器来传递所需的变量来解决。

router.route('/report') // the REST api address
    .post(calling_a_POST)

function calling_a_POST(req, res) {
    ...
    var data = "";
    https.get(url, function callback(response) {
        ...
        response.on("end", function(err, data){       // take these arguments
            response_on_end(err, data, res);          // plus the needed variables
        });
        response.on("error", console.error);
    });
}

function response_on_end(err, data, res) {  // and pass them to function defined outside
    ...
    for (var i = 0; i < length; i++) {
        var report = new Report(array.pop());
        ...
        Report.find({ id: report['id'] })
            .count(function(err, count){
                Report_find_count(err, count, report, res);  // same here
            });
    };
    res.json({                                        // res is now available
        message: 'Grabbed Report'
    });
}

function Report_find_count(err, count, report, res) {        // same here
    ...
    if (count == 0) {
        report.save(function(err) {                   // report is now available
            console.log('saved');
            if (err)
                res.send(err);                        // res is now available
        });
    }
}

When I execute the response_on_end function, I am getting the undefined:1 unexpected token u error. I am pretty much sure it has something to do with this line: var jsonData = JSON.parse(data) My response_on_end is as below: var jsonData = JSON.parse(data); // problem here

我意识到我在这里犯了一个错误:

function calling_a_POST(req, res) {
    ...
    var data = "";
    https.get(url, function callback(response) {
        ...
        //sponse.on("end", function(err, data){
        response.on("end", function(err){ // data shouldn't be here
            response_on_end(err, data, res);
        });
        response.on("error", console.error);
    });
}

另一个我可以预见的问题,实际上可能不会出现在这里,但无论如何还是要谈谈。 data 变量,因为它是一个字符串,与对象不同,它是一个原始类型,所以它是 "passed by value"。 More info

最好将变量包裹在对象中并传递对象,因为javascript中的对象总是"passed by reference".

function calling_a_POST(req, res) {
    ...
    // var data = ""; // 
    var data_wrapper = {};
    data_wrapper.data = {};                                // wrap it in an object
    https.get(url, function callback(response) {
        ...
        response.on("data", function(chunk){
            data_wrapper.data += chunk.toString() + "";   // use the dot notation to reference
        });
        response.on("end", function(err){ 
            response_on_end(err, data_wrapper, res);      // and pass that object
        });
        response.on("error", console.error);
    });
}

function response_on_end_callback(err, data_wrapper, res) {
    var data = data_wrapper.data;                         // later redefine the variable
    ...
    for (var i = 0; i < length; i++) {
        var report = new Report(array.pop());
        ...