Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse SDK JavaScript with Node.js ENOTFOUND

So I want to fetch a large amount of data on Parse, a good solution I found is to make a recursive function: when the data are successfully found, launch another request. The way I'm doing it is pretty simple:

var containedLimit = 1000, // Also tried with 500, 300, and 100
    parseUsersWaiting = {
        // A lot of Users
    },
    parseUsers = {}, // Recipt for Users
    getPlayers = function (containedIn) {        
        var count = 0;

        if (containedIn == undefined) {
            containedIn = [];

            for (var i in parseUsersWaiting) {
                count++;
                if (count > containedLimit) {
                    break;
                }

                containedIn.push(parseUsersWaiting[i].id);
                delete parseUsersWaiting[i];
            }
        }

        var UserObject = Parse.Object.extend('User'),
            UserQuery = new Parse.Query(UserObject);

        UserQuery.limit(containedLimit);

        UserQuery.containedIn('objectId', containedIn);
        UserQuery.find({
            success: function (results) {
                if (results) {
                    for (var i in results) {
                        if (parseUsers[results[i].id] == undefined) {
                            parseUsers[results[i].id] = results[i];
                        }

                        // Other stuff

                        if (results.length < containedLimit) {
                            // End of process
                        } else {
                            getPlayers();
                        }
                    }
                } else {
                    // Looks like an end of process too
                }
            }, error: function (error) {
                console.log(error.message);
                getPlayers(containedIn);
            }
        });
    };

Now, here is what I would like to call the "issue": it happen, very frequently, that the "error" callback is launched with this:

Received an error with invalid JSON from Parse: Error: getaddrinfo ENOTFOUND api.parse.com
at errnoException (dns.js:44:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)

(With code 107, of course) So I searched on the Parse Documentation, and it says the exact same thing: "Received an error with invalid JSON". Yeah.

I'm using the Parse SDK provided by Parse.com (npm install parse)

I also tried to modify a bit the Parse code with replacing the host key by 'hostname' on line 361 of the file parse/node_modules/xmlhttprequest/lib/XMLHttpRequest.js (Parse package version: 1.5.0), and it didn't worked, so I removed my changes.

(By the way, I found a solution talking about using ulimit to change memory usage limit that could solve the problem, but actually, I haven't the necessary rights to execute this command)

like image 287
Edwin Dayot Avatar asked Aug 18 '15 11:08

Edwin Dayot


People also ask

How do you solve error Getaddrinfo Enotfound?

The error getaddrinfo ENOTFOUND localhost is caused by Webpack cannot found localhost address. To solve it, open the terminal: sudo nano /etc/hosts. Add following into the hosts file and save it.

What does error getaddrinfo ENOTFOUND mean?

getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without http: var optionsget = { host : 'localhost', port : 3010, path : '/quote/random', // the rest of the url with parameters if needed method : 'GET' // do GET };

What does Enotfound mean?

If you're trying to connect to localhost , and the ENOTFOUND error is thrown, it may mean that the localhost is missing in your hosts file.

What is parse server in node JS?

Parse Server is an open source backend that can be deployed to any infrastructure that can run Node. js. You can find the source on the GitHub repo. Parse Server uses MongoDB or PostgreSQL as a database. You can deploy and run Parse Server on your own infrastructure.


1 Answers

This error occurs when it can not connect to the API (technically when it cannot lookup the IP address of the API server). Perhaps your internet connection was lost for a brief moment or their SDK server were unavailable (or maybe the server is denying your request due to rate limits).

Either way it is good to code some resilience into your application. I see your on error function retries the API call, but perhaps it would be worth adding a timeout before you do that to give the problem a chance to recover?

    error: function (error) {
        console.log(error.message);
        setTimeout(getPlayers, 10000, containedIn);
    }

If the sever is rate limiting your requests, this will also help.

like image 109
smremde Avatar answered Oct 03 '22 02:10

smremde