我有2个问题:我试图在代码中插入Promise.all,我需要Promise.all进行异步数据库调用。
第一个问题是,通过使用我将在下面发布的代码(代码A),当我登录“ idArticolo”变量时,在控制台中结果是“ getIdArticolo()”函数的所有代码,(我的意思是它只是将我记录在要执行的书面代码中,而不是我想要的变量的值中),也许我不必使用getIdArticolo函数,而是使用一个变量?
我尝试使用变量var getIdArticolo = new Promise
(代码B),它给了我另一个错误:
“ TypeError:无法读取未定义的属性'id'”,指的是代码“ resolve(结果[0] .id);“,”结果“未定义!
为什么?
实际上,它是完全相同的代码,除了一个在一个函数中而另一个在变量的赋值中。显然,我使用了A代码或B代码,对我不想执行的代码进行了注释。
所有代码:
var mysql = require('mysql')
var http = require('http')
var url = require('url');
var express = require("express");
var cors = require('cors')
var app = express();
var sql = "";
var connection = mysql.createConnection({
host: '127.0.0.1',
user: 'andrea',
password: 'password',
database: 'spesa'
});
connection.connect();
function LinkMysql() {
var data = '';
return new Promise(function(resolve,reject) {
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result);
});
});
}
var idArticolo;
var idCategoria;
var nuovoId;
var articolo, costo, quantita, negozio, data;
app.use(cors());
app.get('/inserisciDati',function(request, response){
console.log("/inserisciDati");
/*
console.log("/inserisciDati");
console.log("articolo="+request.query.articolo);
console.log("costo="+request.query.costo);
console.log("quantita="+request.query.quantita);
console.log("negozio="+request.query.negozio);
console.log("data="+request.query.data);
*/
articolo = request.query.articolo;
costo = request.query.costo;
quantita = request.query.quantita;
negozio = request.query.negozio;
data = request.query.data;
//INSERIRE RISPOSTA ALLA PAGINA HTML CON ERRORE SE C'È
//recuperare gli id dell' articolo e del negozio, l' id categoria dell'articolo e il nuovo id del registro
getValues();
//INSERIRE I DATI IN TABLLA MYSQL
});
function getValues(){
Promise.all([getIdArticolo, getIdCategoria, getLastId]).then(function(values){
idArticolo = values[0];
idCategoria = values[1];
nuovoId = values[2];
console.log("idArticolo="+idArticolo);
sql="INSERT INTO registro VALUES ('" + nuovoId + "','" + articolo + "','" + idCategoria +
"','" + idArticolo + "','" + costo + "','" + quantita +
"','" + negozio + "','" + data + "')";
//console.log("sql="+sql);
});
}
var getIdArticolo = new Promise(function(resolve, reject){
sql = "SELECT id FROM articoli WHERE articolo = '" + articolo + "'";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id);
});
});
function getIdArticolo(){
return new Promise(function(resolve,reject) {
sql = "SELECT id FROM articoli WHERE articolo = '" + articolo + "'";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id);
});
});
}
function getIdCategoria(){
return new Promise(function(resolve, reject){
sql = "SELECT id_categoria FROM articoli WHERE articolo = '" + articolo + "'";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id_categoria);
});
});
}
function getLastId(){
return new Promise(function(resolve, reject){
sql = "SELECT id FROM registro ORDER BY id DESC LIMIT 1";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id);
});
});
}
app.get('/getArticoli', function(request, response){
console.log("/getArticoli");
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", "application/json");
sql = 'SELECT articolo FROM articoli ORDER BY articolo ASC';
LinkMysql().then(function(val) {
response.writeHead(200, {'content-Type': 'text/plain; charset=utf-8'});
response.end(JSON.stringify(val));
});
});
app.get('/getNegozi', function(request, response){
console.log("/getNegozi");
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", "application/json");
sql = 'SELECT negozio FROM negozi ORDER BY negozio ASC';
LinkMysql().then(function(val) {
response.writeHead(200, {'content-Type': 'text/plain; charset=utf-8'});
response.end(JSON.stringify(val));
});
});
app.listen(8080);
代码A的摘录:
function getIdArticolo(){
return new Promise(function(resolve,reject) {
sql = "SELECT id FROM articoli WHERE articolo = '" + articolo + "'";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id);
});
});
}
代码B的摘录:
var getIdArticolo = new Promise(function(resolve, reject){
sql = "SELECT id FROM articoli WHERE articolo = '" + articolo + "'";
connection.query(sql, function(err, result) {
if(err)
console.log("error: " + err);
resolve(result[0].id);
});
});
谢谢!
答案 0 :(得分:2)
这两种方法似乎都存在一些问题:
您似乎忘记了调用返回承诺的函数。
例如:代码A为Enums.cs
,必须调用getIdArticolo
才能返回承诺,您可以在getIdArticolo()
语句中等待。
Promise.all
此外,请记住,在像
Promise.all([getIdArticolo(), getIdCategoria()]).then(result => {
});
中将某些内容声明为变量时,变量声明为hoisted。