0

I tried without success, to execute my Promise with Sequelize on stored procedure(MSSQL) inside the function, below the code (when I don´t use the Promise, this code runs perfectly, retrieving the json´s data):

function getData(numTravessia) {
    return new Promise((resolve, reject) => {
        return connection.query(
            'EXEC [dbo].[SP_RECUPERAINFOTRAVESSIA] @ID=:param1',
            {
                replacements: {
                    param1: numTravessia,
                },
                type: QueryTypes.SELECT

            },
            (error, meta, body) => {
                if (body != undefined) {
                    resolve(body.toString());
                } else {
                    reject(error);
                }


            })

    });

}

async function getInfoTravessia(numTravessia) {
    try {
        var r = await getData(numTravessia);
        return JSON.parse(r);;
    } catch (error) {
        console.log(error);
    }
}

app.listen(8000, () => {
    console.log("aplicativo em execução");

    getInfoTravessia(1955).then((result) => {
        console.log(result);
    }).catch((e) => {
        console.log(e);
    })

});

Below, follow the code snippet that I don´t use the Promise and it´s working:

    connection.query(
        'EXEC [dbo].[SP_RECUPERAINFOTRAVESSIA] @ID=:param1',
        {
            replacements: {
                param1: 1955,
            },
            type: QueryTypes.SELECT

        }).then((result) => {
            // RETORNA UM STRING
            var obj = result[0];
            res.send(obj);
            // return obj;

        }).catch((e) => {
            console.log('error', e);
        });

Please, anyone can help me?

Ediberto
  • 3
  • 3
  • why are you returning `connection.query` and not simply resolving the promise? – fedesc Aug 09 '20 at 08:03
  • @fedesc. Do you have a example about this? My "connection" is a instance from Sequelize's config, where I set the database, user, password, schema, etc, – Ediberto Aug 09 '20 at 08:12
  • try removing `return` from `return connection.query` – fedesc Aug 09 '20 at 09:46
  • 1
    @fedesc That `return` doesn't matter, the promise constructor ignores the return value of the executor callback. – Bergi Aug 09 '20 at 12:25
  • Can you show the working code, please? – Bergi Aug 09 '20 at 12:26
  • Possibly better to write `if (error) { reject(error); } else { resolve(body.toString()); }` – Roamer-1888 Aug 09 '20 at 19:19
  • @fedesc, I tried removing the ```return``` follow that you say but is not working. – Ediberto Aug 10 '20 at 12:19
  • @Bergi , looks the second code snippet above which I did edit, according you asked. – Ediberto Aug 10 '20 at 12:31
  • @Roamer-1888 I move the code that you say, but it´s continue not working. – Ediberto Aug 10 '20 at 12:37
  • Oh, `connection.query` already does return a promise if you don't pass a callback? Then you should [avoid the `new Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572)! Keep it as is, maybe just move the query into that helper function and just `return` the promise you get from it. Two major differences between the working code and the broken code though: `body.toString()` and `JSON.parse(r)` – Bergi Aug 10 '20 at 12:42
  • Btw, you should remove the error "handling" from `getData` and `getInfoTravessia`, logging the error and returning `undefined` is not useful. Just let the error bubble, and `.catch()` it only on your main call. – Bergi Aug 10 '20 at 12:44
  • @Bergi do you have an idea that I create this helper function ? I´m searching any example without success. – Ediberto Aug 10 '20 at 15:37
  • You already did, `function getData(numTravessia) {` was fine, you just need to fix the implementation. – Bergi Aug 10 '20 at 15:42
  • @Bergi, it´s works correctly now, Using the Async / Await, It´s not bad, but the implementation retrieve the data. Thanks. – Ediberto Aug 11 '20 at 17:55

0 Answers0