【问题标题】:How to iterate thru array in object, updating object with database calls for each array value?如何遍历对象中的数组,使用每个数组值的数据库调用更新对象?
【发布时间】: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


    【解决方案1】:

    本地对象 = { "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 库似乎不是一个选项。

    //传入的对象是'lo'(即本地对象)

    //遍历每个对象属性 for(var a = 0; a

    //获取每个属性的固定值列表 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;
            }
        })
    

    } } 返回 (lo)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2019-02-12
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多