【发布时间】:2019-06-26 21:46:26
【问题描述】:
使用 NodeJS,尝试使用存储在对象模型中的其他值来更新对象。该对象包含一个数组,其值用于从 MariaDB 查询一组其他值,然后更新原始对象。 Node 依次遍历所有数组值,然后依次遍历每个 db 调用,然后返回对象,但没有更新。
local object = {
"type":null,
"id":null,
"code":null,
"title":null,
"description":null,
"attrs":[
{
"attr_id":null,
"attr_data_type":null,
"attr_control_type":null,
"fixed_list":null,
"attr_fixed_list_values":[
{
"attr_id":null,
"value_id:":null,
"fixed_list_value":null
}
],
"attr_name":null,
"attr_value":null
}
],
"mv_attrs":[
{
"attr_id":null,
"attr_data_type":null,
"attr_name":null,
"attr_values":[]
}
]
};
回调不起作用。嵌套回调不起作用。 Q 库似乎不是一个选项。
// Passed in object is 'lo' (i.e. local object)
//iterate through each object attribute
for(var a = 0; a < Object.keys(lo.attrs).length-1; a++) {
//get fixed value list for each attribute
if (lo.attrs[a].fixed_list === 'Y') {
const sql = 'select aflv.attr_id, aflv.value_id, aflv.attr_value as fixed_list_value from attr_fixed_list_values aflv where aflv.attr_id = ' + lo.attrs[a].attr_id;
connection.query(sql, function(err, rows) {
if (err) {
throw err;
} else {
lo.attrs[a]["attr_fixed_list_values"]=rows;
}
})
}
}
return (lo); //at this point, lo has been updated
目标是填充固定值数组
"attr_fixed_list_values":[
{
"attr_id":null,
"value_id:":null,
"fixed_list_value":null
}
],
对于 attrs 数组中的每个属性。
【问题讨论】:
标签: arrays node.js database loops