【发布时间】:2021-02-16 16:40:27
【问题描述】:
我正在用 NodeJS 编写代码,并且有一个如下所示的对象。
const data =
{
"error1": {
"7": [
{
"ErrorType": "Error-1A",
"Hostnames": "host123.com,hostabc.com,host33a.com..."
}
],
"8": [
{
"ErrorType": "Error-1B",
"Hostnames": "host223.com,host2c.com,host43a.com..."
},
{
"ErrorType": "Error-1C",
"Hostnames": "host1231.com,host2abc.com,host313a.com..."
}
]
},
"error2": {
...
},
"error3": {
...
}
}
我有一个函数fetchHostDetails(hostList),它将hostList 作为输入并返回如下响应:
[
{
"resType": "unknow data",
"res": "missing data"
},
{
"resType": "login failed",
"res": "login with wrong userid"
}
....
]
到目前为止,我已经编写了以下代码,并且按预期工作。
for (let key in data) {
for (let number in data[key]) {
data[key][number].map( async d=> {
const fetchResponse = await sequelize
.query(await fetchHostDetails(d.Hostnames), options);
});
}
}
async function fetchHostDetails(hostList) {
...
return fetchResponse;
}
我想将函数 fetchHostDetails(hostList) 的响应嵌入到 data 并确保在 data 中的 Hostnames 之后显示该响应
我已经编写了以下代码,但它没有更新 data 对象
for (let key in data) {
for (let number in data[key]) {
data[key][number].map( async d=> {
const fetchResponse = await sequelize
.query(await fetchHostDetails(d.Hostnames), options);
data[key][number]['Fetchresult'] = fetchResponse;
});
}
}
return data;
期望的输出:
{
"error1": {
"7": [
{
"ErrorType": "Error-1A",
"Hostnames": "host123.com,hostabc.com,host33a.com...",
"Fetchresult": [
{
"resType": "unknow data1A",
"res": "missing data"
},
{
"resType": "login failed1A",
"res": "login with wrong userid"
}
]
}
],
"8": [
{
"ErrorType": "Error-1B",
"Hostnames": "host223.com,host2c.com,host43a.com...",
"Fetchresult": [
{
"resType": "unknow data1B",
"res": "no record"
},
{
"resType": "login failed1B",
"res": "wrong response"
}
]
...
},
{
"ErrorType": "Error-1C",
"Hostnames": "host1231.com,host2abc.com,host313a.com...",
Fetchresult": [
{
"resType": "error response1C",
"res": "response details"
},
{
"resType": "login failed1C",
"res": "response details"
}
]
...
}
]
},
"error2": {
...
},
"error3": {
...
}
}
【问题讨论】:
标签: javascript node.js arrays json collections