Jonathan Sadow
Jonathan Sadow

Reputation: 11

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

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":[]}

I've tried about 50 different version to fix this. 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.

Upvotes: 1

Views: 306

Answers (2)

Timothy Walters
Timothy Walters

Reputation: 16874

There's no need to wrap Parse.Cloud.run() in a promise, as it already returns a promise.

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.

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

Upvotes: 1

Héctor Ramos
Héctor Ramos

Reputation: 9258

That sounds about right - you're logging the value of 'promise' to the console. You have to actually resolve() the promise to have it call your Cloud Function and so on.

Upvotes: 0

Related Questions