Function does not arrive at the endpoint - Ionic 3 + API NodeJS

0

In an app in Ionic 3 I have this function validateCupom () which when called sends a GET to, in case it has return 200 (OK), then calls the other function setConfig ().

Belowisthefunctionthatneedstobecalledifthefirstonesucceeds.

Attheend,thecodethatthefunctionSetConfiguration()mustrunintheAPI.

Inthedebugconsoleoftheapplication,itappearsthatthestatusinthevalidateCupomwas200,itmakeschangestodatabasefields,everythingworking,butwhenitarrivestogetintotheConfigurationsetitpopsthiserror.

    
asked by anonymous 18.11.2018 / 08:57

1 answer

0

Dude, you're putting two returns in the same method. The TypeError is in 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 Exception of TypeError and if it passes this TypeError it will enter:

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

That's probably what's happening.

    
18.11.2018 / 14:38