i'm trying to get some SQL connections to go on, but everytime I'm making a new request it keeps throwing an error:
RequestError: Requests can only be made in the LoggedIn state, not the Final state
I'm basically using these 3 functions for requests:
function executeStatementMultipleInsert(array) {
request = new Request(
`INSERT INTO tbs_nome (nome, cod)
VALUES (@nome, @nome_cod)
INSERT INTO tbs_sobrenome (sobrenome, cod)
VALUES (@sobrenome, @sobrenome_cod)
INSERT INTO tbs_email (email, cod)
VALUES (@email, @email_cod)
`,
function (err) {
if (err) {
console.log(err);
}
}
);
request.addParameter("nome", TYPES.NVarChar, array[0].nome);
request.addParameter("nome_cod", TYPES.BigInt, array[0].cod);
request.addParameter("sobrenome", TYPES.NVarChar, array[1].sobrenome);
request.addParameter("sobrenome_cod", TYPES.BigInt, array[1].cod);
request.addParameter("email", TYPES.NVarChar, array[2].email);
request.addParameter("email_cod", TYPES.BigInt, array[2].cod);
request.on("requestCompleted", () => {
databaseConnection.close();
});
databaseConnection.execSql(request);
}
// Function to get the soma from queries
async function getSoma(tableName, cod) {
return tp
.sql(
`DECLARE @tableName varchar(1000)
SET @tableName = 'SELECT soma from tbs_cod_${tableName} WHERE cod=${cod}'
EXEC (@tableName)`
)
.execute()
.then((results) => {
// do something with the results
// console.log(results);
databaseConnection.close();
// console.log("database was closed");
return results[0].soma;
})
.fail(function (err) {
// do something with the failure
console.log("nay" + err);
});
}
async function getFinalResults(total) {
console.log(total);
let result = "";
request = new Request(
`SELECT a.animal, c.cor, p.pais
FROM tbs_animais AS a
JOIN tbs_cores AS c ON a.total=c.total
JOIN tbs_paises AS p ON a.total=p.total
LEFT JOIN tbs_cores_excluidas AS ce ON a.total=ce.total
WHERE a.total=@total`,
function (err) {
console.log("hey db");
databaseConnection.close();
if (err) {
throw err;
}
}
);
request.addParameter("total", TYPES.BigInt, total);
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log('not null');
result+= column.value + " ";
}
});
console.log('hey');
console.log('the result is ' + result);
result ="";
});
request.on('done', function(rowCount, more) {
console.log(rowCount + ' rows returned');
});
databaseConnection.execSql(request);
}
const databaseConnection = new Connection(config);
while in my app.js I call them in some chain then statements. only the first post of the server gets inserted correctly, and when I get to my final request, a SELECT in the "getFinalResults" function, it throws me that error. This is some of my app.js file
app.get("/", (req, res) => {
res.render("index");
});
// Post submit route
app.post("/submit", (req, res) => {
let codesArray = [
{
nome: req.body.nome,
},
{
sobrenome: req.body.sobrenome,
},
{
email: req.body.email,
},
];
let codesSum;
// post para a API para obter o array de código
axios
.post("http://138.68.29.250:8082", qs.stringify(req.body), {
headers: { "Content-Type": "application/x-www-form-urlencoded" },
})
.then(async (response) => {
codesSum = response.data.split(/[A-Za-z]*#/gi).filter(Boolean);
codesArray.map((element, index) => {
return (element.cod = Number(codesSum[index]));
});
// executeStatementMultipleInsert(codesArray);
console.log(codesArray);
})
.then(async () => {
for (let object of codesArray) {
let soma = await getSoma(Object.keys(object)[0], object.cod);
// console.log(soma);
codesSum.push(soma);
}
// console.log(codesSum);
let total = await codesSum
.map((num) => Number(num))
.reduce((acc, cv) => acc + cv);
console.log(typeof total);
let objectFinal = await getFinalResults(total);
// console.log(objectFinal);
})
.catch((err) => {throw err});
// connection to database
databaseConnection.connect((err) => {
if (err) {
console.log("Connection failed");
throw err;
}
});
res.redirect("/");
});
i'm still having lots of trouble with asynchronicity and I suspect some of the issure might come from there? Or when I'm connecting and closing the connection to the database, which I'm also not sure is entirely correct