Edit: It is reported that this question is the same as another mail in another thread. Not so; mine refers to specific code, the other is general.
I'm trying to implement a Promise for getValues (), I'm not referring to PromiseAll, but to the simple Promise of getValues. The problem is that "message" in getValues().then... is undefined so I believe that Promise is not doing its job, where is it wrong?
app.get('/inserisciDati',function(request, response){
console.log("/inserisciDati");
articolo = request.query.articolo;
costo = request.query.costo;
quantita = request.query.quantita;
negozio = request.query.negozio;
data = request.query.data;
getValues().then(function(){
console.log("message=" + message);
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
response.setHeader("X-Powered-By",' 3.2.1');
response.setHeader("Content-Type", "text/html");
// risposta alla pagina richiedente obbligatoria
// se non si risponde la funzione app.get("inserisciDati" verrà chiamata continuamente
response.writeHead(200, {'content-Type': 'text/plain; charset=utf-8'});
response.end(message);
});
});
// recupera l'id articolo, l' id categoria dell'articolo e genera l' id per il nuovo inserimento
function getValues(){
return new Promise ((resolve, reject) => {
Promise.all([getIdArticolo(), getIdCategoria(), getLastId()]).then(function(values){
idArticolo = values[0];
idCategoria = values[1];
nuovoId = values[2]+1;
sql="INSERT INTO registro VALUES ('" + nuovoId + "','" + articolo + "','" + idCategoria +
"','" + idArticolo + "','" + costo + "','" + quantita +
"','" + negozio + "','" + data + "')";
console.log("sql="+sql);
connection.query(sql, function(err, result) {
if(err)
message = ("error:"+ err);
else
message = "OK";
});
});
resolve();
});
}