现在,我正在使用一个宁静的API,对于这种特定的路由,我需要使用来自两个不同的现有API的数据,这些数据我合并到一个对象数组中,并在GET路由中作为JSON对象返回。>
因此,在这种情况下,我从某个API获取上层类,然后根据第一个API的值从另一个API获得下层类。为此,我做了一个嵌套的forEach循环,但是不存在添加到第一个API结果中的对象数组。因此,感觉我的代码中应该遍历第二个API调用的部分从未到达。有人知道这里出了什么问题吗?我一直在研究这个问题好几天了,无法弄清楚哪里出了问题。
非常感谢您的帮助,如果描述得太含糊,我很乐意回答您的问题并扩大我的问题。
Br, 维克多
function ObjectHandler(obj: any): object[] {
const returnobject: object[] = [
];
obj.forEach((e: any) => {
const spec = e.codevalue;
const specLC = spec.toLowerCase();
const url: string = "http://baseurlexample.com/" + specLC;
const underclassRAW = testfunction(url);
const underclass: object[] = [
];
underclassRAW.forEach((e: any) => {
const metadata = e.metadata.find((e: any) => e.lang === "EN");
const uc_keyvalues = {
value: e.codevalue,
explanation: metadata.name,
description: metadata.description,
};
underclass.push(uc_keyvalues);
});
const metadata = e.metadata.find( (e: any) => e.lang === "EN");
const keyvalues = {
value: e.codevalue,
explanation: metadata.name,
underclass: underclass,
};
returnobject.push(keyvalues);
});
return returnobject;
}
function testfunction (URL: String): object[] {
const value: object[] = [
];
let data = "";
https.get(URL, (resp: Response) => {
resp.on("data", (chunk: any) => {
data += chunk;
});
resp.on("end", () => {
const newdata = JSON.parse(data);
value.push(newdata);
});
})
.on("error", (err: Error) => {
console.log("Error: " + err.message);
});
return value;
}
returnobject的外观:
[
{
"value": "A",
"explanation": "something",
"underclass": [
{
"value": "A1",
"explanation": "something",
"description": "something."
},
{
"value": "A2",
"explanation": "something",
"description": ""
},
{
"value": "A3",
"explanation": "A3",
"description": "something"
},
{
"value": "A4",
"explanation": "Something",
"description": ""
}
]
},
外观:
[
{
"value": "A",
"explanation": "something",
"underclass": []
},