-1
I’m starting with nodejs and I came across a problem that I’ve been a few days resisting not posting here, but it seems I don’t have enough knowledge and I can’t find material when searching for mssql and Nodejs, so I came to ask for help here.
I need to do a sequence of queries in the database, so I created the following code:
router.post('/importbom', (req, res) => {
const { listaBom } = req.body;
let filter = "";
listaBom.forEach((item, i) => {
sql.close();
if (item) {
const { mpn, descricao } = item;
filter = "CompName LIKE '0%' " + `AND CompID = '${mpn}' `;
if (descricao !== null) {
filter += "OR CompName LIKE '0%' ";
let arrayWord = descricao.split(" ");
arrayWord.forEach((word, i) => {
if (!i) filter += `AND Remark LIKE '%${word.replace('%', '[%]')}%' `;
if (i) filter += `AND Remark LIKE '% ${word.replace('%', '[%]')}%' `;
});
}
sql.connect(vmanagerConfig).then(pool => {
return pool.request()
.query(`SELECT
LTRIM(RTRIM(CompName)) AS ipnProd,
LTRIM(RTRIM(Remark)) AS descricao,
LTRIM(RTRIM(CompID)) AS CompID,
LTRIM(RTRIM(McID)) AS ID,
LTRIM(RTRIM(Amount)) AS amt,
ipnCad = 0,
mnfpartno as mpn
FROM ComponentTrace
WHERE ${filter} and McID <> 9999`).then(result => {
console.log(result.recordset);
});
}).catch(err => {
sql.close();
console.log("--> Erro na linha ..., index.js ", err);
});
sql.on('error', err => {
sql.close();
console.log("--> Erro na linha ..., index.js: ", err);
});
}
});
res.send([]); //por hora não estou usando
})
it receives a list and each row of that list should be a query, only it passes n times by for and returns me only one result.
I used the form indicated, but it closes the connection.
– G.Laveli
on the filter I will improve, however I did not give so much importance to it yet, just wanted this route working, Thanks for trying to help ;)
– G.Laveli
@G.Laveli tries to put pool creation inside the for
– Sorack
Sorry I guess I don’t understand how to do this.
– G.Laveli
@G.Laveli changed in my example
– Sorack
const pool = await sql.Connectionpool(vmanagerConfig); Syntaxerror: await is only Valid in async Function
– G.Laveli
It entered for and generated the following log: https://i.imgur.com/sNtYKso.png
– G.Laveli
@G.Laveli changed the foreach to go
– Sorack
to solve the problem of const pool = await sql.Connectionpool(vmanagerConfig); Syntaxerror: await is only Valid in async, i type async not to look like this: list) => {
– G.Laveli
@G.Laveli made another change to the code
– Sorack
It worked ;) Thanks for helping out.
– G.Laveli