Function does not arrive at endpoint - Ionic 3 + Nodejs API

Asked

Viewed 39 times

0

In an app in Ionic 3 I have this function validarCupom() that when called sends a GET to , if it has the return 200 (OK), then calls the other function setBonificacaoConveniado().

inserir a descrição da imagem aqui

Below is the function that needs to be called if the first is successful.

inserir a descrição da imagem aqui

At the end, the code that the setBonificacaoConveniado() function should run in the API.

inserir a descrição da imagem aqui

In the application’s debug console, it appears that the status in validarCupom was 200, it makes changes in database fields, everything working, but when it arrives to enter the setBonificacaoConveniado it bursts this error.

inserir a descrição da imagem aqui

  • Put in the question the code and not images, there is a button in the Question editor that delimits the code so color taking into account the code regarding the tags.

  • The problem could be res.status(200).send({result}) on line 602 Node only sends a Answer by request and Try below has another query with error Answer that could not be sent even if there is an error, because Answer has already been called.

1 answer

0

Dude, you’re putting two returns in the same method. Typeerror is on the console.log().

app.post("/teste", function(req, res) {
        var array = [];
        try{
            res.status(200).send(excecao);
            try{
                console.log("teste" + array);
            }catch (e) {
                res.status(200).send(excecao);
            }
        }catch (excecao) {
            res.status(200).send(excecao);
        }
    });

When it enters the second Try of the code, it falls into the Typeerror Exception and if you pass this Typeerror will enter the:

Unhandled rejection Error: Can't set headers after they are sent.

That’s probably what’s going on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.