Monday, 8 May 2017

Promises fail for httprequest in nodejs (Parse.com)

I'm trying to implement 3 httprequests in parallel in nodejs on a parse.com sever. I'm still trying to figure out how it works. For the moment, the following codes throws no syntax error but also has returns a single blank console.log in the server logs. Any help is very appreciated.

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



via HymnZ

No comments:

Post a Comment