无法在解析云函数中从httpRequest中检索结果

Unable to retrieve result from httpRequest in Parse Cloud function

本文关键字:httpRequest 检索 结果 函数      更新时间:2023-09-26

我正在运行一个云函数内的Parse httpRequest,我从不同的解析作业调用。我不知道如何从我在Cloud作业中调用的Cloud函数中检索结果值/成功值。

下面是云函数的代码:
Parse.Cloud.define("maps_request", function(request, response) {
  var maps_url = 'http://maps.googleapis.com/maps/api/directions/json';
  var origin = request.params.origin;
  var destination = request.params.destination;
  var waypoint = "via:" + request.params.waypoint;
  Parse.Cloud.httpRequest({
    url: maps_url,
    params: {
      origin: origin,
      destination: destination,
      waypoints: waypoint,
      optimizeWaypoints: true,
      durationInTraffic: true
    },
    success: function(directions) {
      var time = directions.data['routes'][0]['legs'][0]['duration']['value']
      time = time / 60.00
      response.success(time)
    },
    error: function(error) {
      response.error("Shit broke.")
    }
  })
});

和Cloud作业中调用的代码:

var promise = Parse.Promise.as();
      promise = Parse.Cloud.run('maps_request', {
        origin: String(driverOriginAddress + " " + driverOriginZip),
        destination: String(driverDestinationAddress + " " + driverDestinationZip),
        waypoint: String(driverDestinationAddress + " " + driverDestinationZip)
      }, {
        success: function(results) {
          console.log("Results: " + results)
        },
        error: function(){
          console.log("Error")
        }
      })
      console.log(promise)

解析日志显示我想从云函数的确切结果,但我得到这个:"{"_resolved":假的,"_rejected":假的,"_resolvedCallbacks":[],"_rejectedCallbacks":[]}

我尝试了大约50个不同的版本来解决这个问题。如有任何帮助,我将不胜感激。

这是云函数的另一个版本的代码:

Parse.Cloud.define("maps_request", function(request, response) {
  var maps_url = 'http://maps.googleapis.com/maps/api/directions/json';
  var origin = request.params.origin;
  var destination = request.params.destination;
  var waypoint = "via:" + request.params.waypoint;
  var test = []
  Parse.Cloud.httpRequest({
    url: maps_url,
    params: {
      origin: origin,
      destination: destination,
      waypoints: waypoint,
      optimizeWaypoints: true,
      durationInTraffic: true
    },
    success: function(directions) {
      var time = directions.data['routes'][0]['legs'][0]['duration']['value']
      time = time / 60.00
      test.push(time)
    },
    error: function(error) {
      console.log("Error")
    }
  }).then(function() {
    response.success(test[0])
  }, function (error) {
    response.error("Error2")
  })
});

和呼叫:

var test = Parse.Cloud.run('maps_request', {
        origin: String(driverOriginAddress + " " + driverOriginZip),
        destination: String(driverDestinationAddress + " " + driverDestinationZip),
        waypoint: String(driverDestinationAddress + " " + driverDestinationZip)
      })
      console.log("Test: " + test)

httpResponse显示了正确的值,但仍然无法访问它们或分配给变量

没有必要将Parse.Cloud.run()包装在承诺中,因为它已经返回了一个承诺。

同样在你的第二个例子中,你调用云函数,就好像它是一个sync调用而不是async,并查看承诺而不是你想要的结果。

试试这个:

Parse.Cloud.run('maps_request', {
  origin: String(driverOriginAddress + " " + driverOriginZip),
  destination: String(driverDestinationAddress + " " + driverDestinationZip),
  waypoint: String(driverDestinationAddress + " " + driverDestinationZip)
}).then(function (result) {
  // we have the result of the cloud function here, just log it for now
  console.log("Test:", result);
}, function (error) {
  // uh oh!
  console.log(error);
});

这听起来是对的-您正在将'promise'的值记录到控制台。你必须真正的resolve()承诺让它调用你的云函数等等