[英]NodeJS + mysql: Select query returning individual objects
I have been using the mysql
NPM package for many months and have not faced any issues. 我已经使用
mysql
NPM软件包很多月了,没有遇到任何问题。 But this code is acting strange. 但是这段代码表现得很奇怪。
using(connectionPool.getSqlConnection(), function(connection) {
return connection.queryAsync('SELECT * from table_name');
}).spread(function(rows, fields) {
console.log('Rows: %j', rows);
console.log('Fields: %j', fields);
res.json(rows);
}).error(function(err) {
console.log(err);
});
I know that there are 50 rows in the table. 我知道表中有50行。 But I am getting only 1 in
rows
. 但是我只得到1
rows
。 Also, strangely fields
has the 2nd row. 此外,奇怪的是
fields
具有第二行。 Any idea why this is happening? 知道为什么会这样吗? I have used the same query elsewhere with no problems.
我已经在其他地方使用了相同的查询,没有问题。
EDIT 编辑
I fiddled around a little bit and found that each row is coming as a separate object. 我摆弄了一下,发现每一行都是一个单独的对象。 So if I change the code to the following:
因此,如果我将代码更改为以下内容:
using(connectionPool.getSqlConnection(), function(connection) {
return connection.queryAsync('SELECT * from table_name');
}).spread(function(r1, r2, r3, r4) {
console.log('Rows: %j', r1);
console.log('Rows: %j', r2);
console.log('Rows: %j', r3);
console.log('Rows: %j', r4);
res.json(rows);
}).error(function(err) {
console.log(err);
});
each of r1, r2, r3, r4, ... have a single row object. r1,r2,r3,r4,...中的每个都有一个行对象。
Answering myself so that it helps someone else. 回答自己,以帮助他人。
using(connectionPool.getSqlConnection(), function(connection) {
return connection.queryAsync('SELECT * from table_name');
}).then(function(rows) {
console.log('Rows: %j', rows);
res.json(rows);
}).error(function(err) {
console.log(err);
});
This works. 这可行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.