0
How can I store the result of Promise in a variable? Promise returns the result of a database query.
function consultaMarcas(){
return new Promise(function(resolve, reject) {
connection.query('SELECT * FROM marca',(err, result)=>{
if(err) return reject (err);
else return resolve(result);
});
});
};
var marcas = consultaMarcas().then(result =>{
}).catch(err =>{
console.log("ERRO: ",err);
});
My problem is a little bigger, because I need to perform more than one query and I need to pass this to front as an example, here is what I would have on the server:
function recebendoValoresBD() {
Promise.all([
consultaMarcas().then(),
consultaTipos().then()
])
.then(result =>{
const marcas = {marca: result[0]};
const tipos = {tipo: result[1]};
res.render('marcas', marcas, tipos);
})
.catch(err =>{
console.log("ERRO: ", err);
});
}
And here’s the page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cadastro Modelo</title>
</head>
<br/>
<h1>Cadastro Modelo</h1>
<hr/>
<form action="/modelos" method="post">
Marcas:<SELECT NAME="ativo">
<option><%=marcas.marca[0].descricao%>
</SELECT>
<br>
Tipos:<SELECT NAME="ativo">
<option><%=tipos.tipos[0].descricao%>
</SELECT>
<br>
Descrição:<input type="text" name="descricao"/><br/>
Ativo: <SELECT NAME="ativo">
<option>
<option>S
<option>N
</SELECT>
<button type="submit" class="btn btn-primary" >Proximo</button>
</form>
</body>
</html>
But doing so it returns me an error that is: Typeerror: callback is not a Function
Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).
– Sorack