【发布时间】:2017-11-15 16:33:08
【问题描述】:
我正在尽力理解 nodeJS/typescript 及其技术。但仍然无法弄清楚如何将我的数据库查询结果保存到变量中并返回它。也许有人可以解释我的问题/帮助我:
我有这样的方法:
public getAllProducts(): ProductArray {
// returns IConnection from "mysql" and connect
this.databaseConnection.getConnection().connect();
var product: any = [];
this.databaseConnection.getConnection().query('SELECT * FROM product', function (error: any, results:any, fields:any) {
if (error) throw error;
product.push(results);
});
console.log(product);
return product;
}
如果有人可以在这里帮助我,那就太好了。
【问题讨论】:
-
返回产品应该在回调函数内,即 function (error: any, results:any, fields:any) { if (error) throw error; product.push(results);}
标签: javascript mysql node.js typescript callback