I'm trying to do the following in Parse:
Create a cloud function that invokes a http request and the cloud function then returns this response from the http request, what would be the correct way of doing this as I am getting errors with this approach, I think I am using the concept of promises in the wrong way.
Parse.Cloud.define('test_function', function(req, res){
var myData = {}
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://dummyurl',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: {
some_data : "test_data"
}
}).then(function(httpResponse) {
console.log(httpResponse.text);
myData = httpResponse.data;
}, function(httpResponse) {
console.error('Request failed with ' + httpResponse.status);
res.error("Request failed");
});
res.success(myData);
});
since you are returning JSON data you can simply send it in the response object.Also you should call response.success after your block has been executed and not right after you execute it So in your case your code should look like the following:
Parse.Cloud.define('test_function', function(req, res) {
var myData = {}
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://dummyurl',
headers: {
'Content-Type': 'application/json;charset=utf-8'
},
body: {
some_data: "test_data"
}
}).then(function(httpResponse) {
console.log(httpResponse.text);
myData = httpResponse.data;
res.success(myData); // this should be called in here!
}, function(httpResponse) {
console.error('Request failed with ' + httpResponse.status);
res.error("Request failed");
});
});