我根据问题的答案使用了queryAsync()
,但它确实有效,但它为我的查询结果添加了大量额外的元数据,否则这是一个简单的查询。
以下是我用来处理/执行查询并记录结果的内容:
var retrieveSettings = Promise.method(function (username, connection) {
console.log('User ' + username + ' retrieving settings...');
var q = 'select * from sales_settings';
return connection.queryAsync(q).then(function (rows, fields) {
list = [];
for (x = 0; x < rows.length; x++) {
list.push(rows[x]);
}
//console.log('Settings: ' + JSON.stringify(list, null, 4));
return list;
});
});
以下是记录的结果:
Settings: [
[
{
"group_name": "add_auto",
"commission_rate": 0,
"monthly_req": 0,
"category_name": "Auto",
"commission_type": "none",
"group_title": "Added Auto"
},
{
"group_name": "add_fire",
"commission_rate": 0,
"monthly_req": 0,
"category_name": "Fire",
"commission_type": "none",
"group_title": "Added Fire"
},
{
"group_name": "bank_dep",
"commission_rate": 25,
"monthly_req": 0,
"category_name": "Bank",
"commission_type": "static",
"group_title": "Bank Deposit"
},
{
"group_name": "bank_loan",
"commission_rate": 75,
"monthly_req": 8,
"category_name": "Bank",
"commission_type": "static",
"group_title": "Bank Loan"
},
{
"group_name": "health",
"commission_rate": 0.084,
"monthly_req": 4,
"category_name": "Health",
"commission_type": "premium",
"group_title": "Health App"
},
{
"group_name": "life",
"commission_rate": 0.084,
"monthly_req": 8,
"category_name": "Life",
"commission_type": "premium",
"group_title": "Life App"
},
{
"group_name": "new_auto",
"commission_rate": 0.03,
"monthly_req": 32,
"category_name": "Auto",
"commission_type": "rate",
"group_title": "Raw New Auto"
},
{
"group_name": "new_fire",
"commission_rate": 0.03,
"monthly_req": 20,
"category_name": "Fire",
"commission_type": "rate",
"group_title": "Raw New Fire"
}
],
[
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "group_name",
"orgName": "group_name",
"filler1": [
12
],
"charsetNr": 33,
"length": 135,
"type": 253,
"flags": 20483,
"decimals": 0,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
},
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "commission_rate",
"orgName": "commission_rate",
"filler1": [
12
],
"charsetNr": 63,
"length": 13,
"type": 246,
"flags": 4097,
"decimals": 3,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
},
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "monthly_req",
"orgName": "monthly_req",
"filler1": [
12
],
"charsetNr": 63,
"length": 11,
"type": 3,
"flags": 4097,
"decimals": 0,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
},
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "category_name",
"orgName": "category_name",
"filler1": [
12
],
"charsetNr": 33,
"length": 135,
"type": 253,
"flags": 4097,
"decimals": 0,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
},
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "commission_type",
"orgName": "commission_type",
"filler1": [
12
],
"charsetNr": 33,
"length": 135,
"type": 253,
"flags": 4097,
"decimals": 0,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
},
{
"catalog": "def",
"db": "officeball",
"table": "sales_settings",
"orgTable": "sales_settings",
"name": "group_title",
"orgName": "group_title",
"filler1": [
12
],
"charsetNr": 33,
"length": 72,
"type": 253,
"flags": 4097,
"decimals": 0,
"filler2": [
0,
0
],
"zeroFill": false,
"protocol41": true
}
]
]
为什么这个不寻常的元数据被添加到我的查询结果中?
答案 0 :(得分:2)
看起来您正在使用蓝鸟,在这种情况下,您可以使用.spread
:
var retrieveSettings = Promise.method(function (username, connection) {
console.log('User ' + username + ' retrieving settings...');
var q = 'select * from sales_settings';
return connection.queryAsync(q).spread(function (rows, fields) {
list = [];
for (x = 0; x < rows.length; x++) {
list.push(rows[x]);
}
//console.log('Settings: ' + JSON.stringify(list, null, 4));
return list;
});
});
mysql模块的问题在于它不符合节点js回调标准(err, result)
。相反,它使用(err, result1, result2)
。因为函数只能抛出一个异常或返回一个值,所以bluebird返回一个[result1, result2]
数组以避免信息丢失。
.spread
与.then
类似,不同之处在于它假设完整值是一个数组并将数组值传播到参数上。
答案 1 :(得分:1)
来自mysql query()
的标准回调返回rows
和fields
。您正在使用的异步包装器(例如,如果您Q.denodeify()
)仅返回单个内容,因此对于具有多个参数的回调,它返回[param1,param2]
的数组,在这种情况下为[rows,fields]
。< / p>
与此相关,then
内的函数只需要接受一个参数(这是[rows,fields]的数组)。
请参阅我的答案的第二部分to this question作为插图。
您的代码应该是这样的:
var retrieveSettings = Promise.method(function (username, connection) {
console.log('User ' + username + ' retrieving settings...');
var q = 'select * from sales_settings';
return connection.queryAsync(q).then(function (results) {
var rows = results[0]; // get the rows
var fields = results[1]; // don't really care, but just for illustration
list = [];
for (x = 0; x < rows.length; x++) {
list.push(rows[x]);
}
//console.log('Settings: ' + JSON.stringify(list, null, 4));
return list;
});
});
实际上,对于上面的代码,没有理由你的整个回调不能只是:
return connection.queryAsync(q).then(function (results) {
return results[0]; // get the rows
});