简体   繁体   English

无法从Parse Cloud功能中的httpRequest检索结果

[英]Unable to retrieve result from httpRequest in Parse Cloud function

I am running a Parse httpRequest inside a Cloud function, which I am calling from a different Parse job. 我正在Cloud函数中运行Parse httpRequest,这是从其他Parse作业调用的。 I cannot figure out how to retrieve the result value / success value from the Cloud function from where I called it within the Cloud job. 我无法弄清楚如何从Cloud函数中从Cloud作业中调用它的位置检索结果值/成功值。

Here's the code for the cloud function: 这是云函数的代码:

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.")

    }
  })
});

And the code for the call in the Cloud job: 以及云端作业中呼叫的代码:

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)

The Parse logs show the exact results I want from the Cloud function, but I get this: "{"_resolved":false,"_rejected":false,"_resolvedCallbacks":[],"_rejectedCallbacks":[]} 解析日志显示了我想要的Cloud函数的确切结果,但是我得到了:“ {” _resolved“:false,” _ rejected“:false,” _ resolvedCallbacks“:[],” _ rejectedCallbacks“:[]}

I've tried about 50 different version to fix this. 我尝试了大约50个不同的版本来解决此问题。 Any help would be MUCH appreciated. 任何帮助将非常感激。

This is another version of the code for cloud function: 这是云功能代码的另一个版本:

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

And the call: 并致电:

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

The httpResponse shows the right value, but still not able to access them or assign to a variable. httpResponse显示正确的值,但仍然无法访问它们或将其分配给变量。

There's no need to wrap Parse.Cloud.run() in a promise, as it already returns a promise. 无需将Parse.Cloud.run()包装在一个Parse.Cloud.run()中,因为它已经返回了Parse.Cloud.run()

Also in your second example you're calling the cloud function as if it is a sync call instead of async and looking at the promise instead of the result you want. 同样在第二个示例中,您正在调用云函数,就好像它是同步调用而不是异步调用一样,并查看了promise而不是所需的结果。

Try this: 尝试这个:

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

That sounds about right - you're logging the value of 'promise' to the console. 听起来不错,您正在将“ promise”的值记录到控制台中。 You have to actually resolve() the promise to have it call your Cloud Function and so on. 您必须实际实现promise()使其调用您的Cloud Function的承诺,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM