javascript - Node JS Promise 的问题

标签 javascript node.js

在使用必要的值更新消息数组之前,我找不到此函数返回的解决方案。

var calculateDistance = function (message, cLongitude, cLatitude, cSessionID) {
  return new Promise(function (resolve, reject) {

    distance.key = options.apiKey;
    distance.units('metric');

    var origins = [];
    origins.push(cLatitude + ',' + cLongitude);

    message.forEach(function (obj) {
      obj.sessionId = cSessionID;
      var destinations = [];
      destinations.push(obj.geoLocation.latitude + ',' + obj.geoLocation.longitude);

      distance.matrix(origins, destinations, function (err, distances) {
        if (err) {
          return console.log(err);
        }
        if (!distances) {
          return console.log('no distances');
        }
        if (distances.status == 'OK') {
          for (var i = 0; i < origins.length; i++) {
            for (var j = 0; j < destinations.length; j++) {
              var origin = distances.origin_addresses[i];
              var destination = distances.destination_addresses[j];
              if (distances.rows[0].elements[j].status == 'OK') {
                var distance = distances.rows[i].elements[j].distance.text;
                console.log('Distance from ' + origin + ' to ' + destination + ' is ' + distance);
                obj.distance = distance;
              } else {
                console.log(destination + ' is not reachable by land from ' + origin);
                obj.distance = 'N/A';
              }
            }
          }
        }
      });

    });

    return resolve(message);
  });
}

有人可以向我指出我在这里做错了什么吗?

问候 吉米

最佳答案

 var async = require('async');
 var calculateDistance = function (message, cLongitude, cLatitude, cSessionID) {
    return new Promise(function (resolve, reject) {

        distance.key = options.apiKey;
        distance.units('metric');

        var origins = [];
        origins.push(cLatitude + ',' + cLongitude);

        async.each(message, function(obj, callback) {
          obj.sessionId = cSessionID;
          var destinations = [];
          destinations.push(obj.geoLocation.latitude + ',' + obj.geoLocation.longitude);

          distance.matrix(origins, destinations, function (err, distances) {
            if (err) {
              callback(err);
            }
            if (!distances) {
              callback('no distances');
            }
            if (distances.status == 'OK') {
              for (var i = 0; i < origins.length; i++) {
                for (var j = 0; j < destinations.length; j++) {
                  var origin = distances.origin_addresses[i];
                  var destination = distances.destination_addresses[j];
                  if (distances.rows[0].elements[j].status == 'OK') {
                    var distance = distances.rows[i].elements[j].distance.text;
                    console.log('Distance from ' + origin + ' to ' + destination + ' is ' + distance);
                    obj.distance = distance;
                  } else {
                    console.log(destination + ' is not reachable by land from ' + origin);
                    obj.distance = 'N/A';
                  }
                }
              }
              callback(null);
            }
            });
          },function(err){
             if(err){
               return reject(err);
          }else{
              return resolve(message);
                }
         });
    });
};

关于javascript - Node JS Promise 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41651214/

相关文章:

node.js - 如何删除filepond上的临时上传文件

node.js - 使用 node-cron 使用 mongoose 更新多个文档

javascript - Uncaught ReferenceError : Rx is not defined

javascript - jQuery 自动调整下拉菜单宽度

javascript - window.frames[0].document.onkeydown 在 Firefox 中不起作用

javascript - 为什么我会收到意外的模板字符串表达式错误?

javascript - 如何在 javascript 中将 if else 条件添加到数组中(已编辑)

node.js - 在 updateOne() 之后获取更新值

javascript - 如何在 node.js 应用程序中正确访问由 express 设置的应用程序变量?

javascript - socket.io 的初学者教程好吗?