create-react-app: "The development server has disconnected. "

Viewed 807

There are several questions about this error, but none of them fit exactly my circunstances. Let me explain: I'm writing some code using create-react-app that makes an AJAX call to an external server:

async ajaxCall(url) {
    var objetorespuesta = null;

    try {
        let respuesta = await fetch(url);
        if (respuesta.ok) {
                objetorespuesta = {
                "status": { status: "ok" },
                "datos": await respuesta.json()
                };
            }
        else {
            objetorespuesta = {
                "status": {
                    status: "error",
                    errorcode: respuesta.status,
                    errortext: respuesta.statusText
                    }
                };      
            }
        }
    catch (e) {
        console.log("Call failed!!");
        objetorespuesta = {
            "status": {
                status: "error",
                errorcode: "Error de red",
                errortext: e
                }
            };                  
        }
    return objetorespuesta;
    }

let url = "https://blablabla" //My URL here; edited out;
const objetorespuesta = this.ajaxCall(url);

for (let key in objetorespuesta) {
    console.log("Mooooo: " + key + ": " + objetorespuesta[key]);
    }

Now, before I added the "try/catch" block, I got in the console an:

Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
The development server has disconnected. Refresh the page if necessary

Which I believe it's because of CORS, since I'm not calling localhost but an external URL. I'll deal with that later, but for the moment I decided to add the "try/catch" so that the code catches that error and can keep executing...

...except that it doesn't do that. When the error happens, nothing gets executed anymore. What I mean is that if the code worked, the network call would fail, but I'd get several "Mooooo" messages in the console with the variables of the returned object. Instead, what I get is:

Call failed!!
The development server has disconnected. Refresh the page if necessary.

It seems that the development server get disconnected right after the network call fails.

What's going on? The development server fails, but the JS code is being executed in my browser, so it should have no influence, right? Or is there some other error in my code that I'm missing?

0 Answers
Related