简体   繁体   中英

Parse cloud code loop through url and httpRequest

I just want to do a simple loop in my "alerts" objects, which contains an url, and a word. For each alert, I do a httpRequest to check if the word is present in the response html code. I yes, I put the status to true.

I also want to update each time the "updatedTo" column, even if I don't find the word in the response html code, but I don't know why...

I wrote this cloud code, but it don't works, or it works sometimes only if I have only items with the word present.

    Parse.Cloud.job("updateStatus", function(request, status) {
    Parse.Cloud.useMasterKey();
    var counter = 0;
    var AlertItem = Parse.Object.extend("Alert");
    var query = new Parse.Query(AlertItem);
    query.each(function(alert) {
            var alertTitle = alert.get("title");
            var alertUrl = alert.get("url");
            var alertStatus = alert.get("status");
            var alertWords = alert.get("research");
            console.log("Alert : " + alertTitle + " - Check if : " + alertWords + " is on : " + alertUrl)

            promise = promise.then(function() {
                    return Parse.Cloud.httpRequest({
                        url: alertUrl,
                        headers: {
                            'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25'
                        },
                    }).then(function(httpResponse) {
                            console.log("We succeded to access to the website");
                            var htmlCode = httpResponse.text;
                            if (htmlCode.indexOf(alertWords) >= 0) {
                                if (alertStatus == false) {
                                    alert.set("status", true);
                                    console.log("new status:true");
                                    return alert.save();
                                }
                            } else {
                                alert.set("status", false);
                                console.log("new status:false");
                                //I do this to updated the "updatedTo" field, but it doesn't work
                                return alert.save();
                            }
                            // You need to return a Promise here if non of the above condition meet.
                        },
                        function(error) {
                            console.error('Request failed with response code ' + httpResponse.headers.Location);
                            // You need to return a rejected promise here.
                        }
                    });
            });
        return promise;
    }).then(function() {
    status.success('Status updated');
    // Set the job's success status
}, function(error) {
    // Set the job's error status
    status.error("Uh oh, something went wrong.");
});
});

The query.each(callback, options) from documentation.

Iterates over each result of a query, calling a callback for each one. If the callback returns a promise, the iteration will not continue until that promise has been fulfilled. If the callback returns a rejected promise, then iteration will stop with that error. The items are processed in an unspecified order. The query may not have any sort order, and may not use limit or skip.

Parse.Cloud.job("updateStatus", function(request, status) {
    Parse.Cloud.useMasterKey();
    var counter = 0;
    var AlertItem = Parse.Object.extend("Alert");
    var query = new Parse.Query(AlertItem);
    query.each(function(alert) {
        var alertTitle = alert.get("title");
        var alertUrl = alert.get("url");
        var alertStatus = alert.get("status");
        var alertWords = alert.get("research");
        console.log("Alert : " + alertTitle + " - Check if : " + alertWords + " is on : " + alertUrl)


        return Parse.Cloud.httpRequest({
            url: alertUrl,
            headers: {
                'user-agent': 'A user classic agent'
            },
            success: function(httpResponse) {
                console.log("We succeded to access to the website");
                var htmlCode = httpResponse.text;
                if (htmlCode.indexOf(alertWords) >= 0) {
                    if (alertStatus == false) {
                        alert.set("status", true);
                        console.log("new status:true");
                        return alert.save();
                    }
                } else {
                    alert.set("status", false);
                    console.log("new status:false");
                    //I do this to updated the "updatedTo" field, but it doesn't work
                    return alert.save();
                }
                // You need to return a Promise here if non of the above condition meet.
            },
            error: function(httpResponse) {
                console.error('Request failed with response code ' + httpResponse.headers.Location);
                // You need to return a rejected promise here.
            }
        });
    }).then(function() {
        status.success('Status updated');
        // Set the job's success status
    }, function(error) {
        // Set the job's error status
        status.error("Uh oh, something went wrong.");
    });
});

So, with any help it was difficult, but I finish to find another post who was close to what I need, I adapt it, and I success to use it, it works great with Promises :) :

var _ = require('underscore.js')

Parse.Cloud.job("updateStatus", function(request, response) {

var alerts = Parse.Object.extend("Alert");
var query = new Parse.Query(alerts);
query.equalTo("status", false);

query.find().then(function(alerts) {

    var promise = Parse.Promise.as();

    _.each(alerts, function(alert) {

        var alertUrl = alert.get("url");
        ...

        promise = promise.then(function() {
            return Parse.Cloud.httpRequest({
                url: alertUrl
                }).then(function(httpResponse) {
                    ...
                }, 
                function(error) {
                    ...
                });
            });
    });

    return promise;

}).then(function() {
    response.success("All status updated with success !");
}, 
function (error) {
    response.error("Error: " + error.code + " " + error.message);
});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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