I'm trying to implement 3 httprequests in series in Nodejs on a parse.com server. I'm still trying to figure out how it works. For the moment, the following codes throws no syntax error but also has a return of a single blank console.log in the server logs.
Parse.Cloud.define('paypalCheck', function (request, response) {
Parse.Cloud.httpRequest({
method: 'POST',
url:url1,
body:body1
}).then(function(httpResponse){
console.log(httpResponse.text.split("\n")[0])
var promises = [];
var updated = false;
if(httpResponse.text.split("\n")[0] == 'FAIL'){
var orderUpdate = Parse.Cloud.httpRequest({
method: 'POST',
url:url2,
headers:header2,
body:body2
}).then(function(httpResponse) {
console.log('updated')
updated = true;
return 'order updated';
});
promises.push(orderUpdate);
}
if(updated){
var orderFetch = Parse.Cloud.httpRequest({
method: 'GET',
url:url3,
headers:header3
}).then(function(httpResponse) {
return_url = httpResponse.data.order.order_status_url
return httpResponse;
});
promises.push(orderFetch);
}
return Parse.Promise.when(promises)
}).then(function(httpResponse){
console.log('result');
console.log(httpResponse);
response.success(return_url)
}, function(error){
console.log('result');
response.error(error)
})
})
Some modifications needed for your code:
Parse.Cloud.define('paypalCheck', function (request, response) {
Parse.Cloud.httpRequest({
method: 'POST',
url:url1,
body:body1
}).then(function(httpResponse){
console.log(httpResponse.text.split("\n")[0])
var promises = [];
var updated = false;
if(httpResponse.text.split("\n")[0] == 'FAIL'){
return Parse.Cloud.httpRequest({
method: 'POST',
url:url2,
headers:header2,
body:body2
}).then(function(httpResponse) {
console.log('updated')
updated = true;
return Parse.Cloud.httpRequest({
method: 'GET',
url:url3,
headers:header3
});
}).then(function(httpResponse) {
return_url = httpResponse.data.order.order_status_url
return httpResponse;
});;
}
else console.log("FAIL was true, do nothing");
}).then(function(httpResponse){
console.log('result');
console.log(httpResponse);
response.success(return_url)
}, function(error){
console.log('result');
response.error(error)
})
})
First off, I've added an else condition to your check on httpResponse.text.split, just so see if it hits.
Second, I've gotten rid of your Parse.Promise.when() call and instead chained the requests you were making so they properly happen in sequence. The way you had it, the if(updated) would never pass, since the check would happen before the second httpRequest ever came back.
Let me know if this changes your output at all.