How can I store the promise result in a variable? A promise returns the result of a query of the database.
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 which is: TypeError: callback is not a function