我正在尝试设置此对象:
var userPref = {}
从此查询返回到mysql:
var qSelect = "SELECT age_min, age_max, max_distance, partner_gender_id, partner_children, partner_family_plans FROM User_Preferences WHERE user_id = ?";
var qValues = [user_pref_id];
var qCall = mysql.format(qSelect, qValues);
connection.query(qCall, function(err, userPrefFound, fields) {
if (err) {
console.log("get my preferences error: " + err);
callback(false);
} else {
userPref = userPrefFound[0];
console.log("userPref", userPref)
if (userPrefFound.length > 0 && userPref.max_distance < 300) {
getUserLocation();
} else {
getUserPrefEth();
}
}
});
我的userPref打印为RowDataPacket
userPref RowDataPacket {
age_min: 18,
age_max: 65,
max_distance: 5,
partner_gender_id: 2,
partner_children: 2,
partner_family_plans: 3 }
然后不允许我像这样设置对象
var politics_interested = [];
var qSelect = "SELECT politics_type FROM User_Pref_Politics WHERE user_id = ?";
var qValues = [user_pref_id];
var qCall = mysql.format(qSelect, qValues);
connection.query(qCall, function(err, userPrefPolitics, fields) {
if (err) {
console.log("get my preferences politics error: " + err);
callback(false);
} else {
if (userPrefPolitics.length > 0) {
userPrefPolitics.forEach(function(i) {
politics_interested.push(i.politics_type);
});
}
console.log("politics_interested", userPref)
userPref["politics_interested"] = politics_interested;
getUserPrefMarital();
}
});
并显示此错误:
\node_modules\mysql\lib\protocol\Parser.js:437
throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot set property 'politics_interested' of undefined
我该如何从mysql获取返回值并将其设置为对象?
我尝试了一个数组userPref ['religious_level_interested'] = JSON.parse(JSON.stringify(userPrefRelig));但是,当我尝试将其用于某个对象时,它将无法正常工作,不确定是否可以正常工作。