Currently, I am developing a RESTful API backend using nodejs with typescript. My task involves extracting information from multiple APIs, parsing the results, and then forwarding the parsed data to the frontend. At present, I am focused on an API route where I gather data from two different external API routes using https. The data is then passed to an ObjectHandler in the format [object Object],[object Object]. This structure comes about because I collect responses from my first http call into one array, responses from my second http call into another array, and then push both arrays onto a third array for combined data.
const first: object [] = [
];
const second: object [] = [
];
const combined: object [] = [
];
The code for my object handler is as follows:
function ObjectHandlerKeywords(obj: any): object[] {
const keywords: object [] = [
];
if (obj instanceof Array) {
obj.forEach((e: any) => {
const results = e.results.map((x: any) => x);
const vals = {
localname: results.localname,
prefLabel: results.prefLabel,
altLabel: results.altLabel,
};
keywords.push(vals);
});
return keywords;
}
}
However, I encounter the error message:
const results = e.results.map((x) => x);
^
TypeError: Cannot read property 'map' of undefined
The sample http response data looks like this, and I aim to extract values from within the "results" object array:
{
"@context": {
"skos": "http://www.w3.org/2004/02/skos/core#",
"isothes": "http://purl.org/iso25964/skos-thes#",
"onki": "http://schema.onki.fi/onki#",
"uri": "@id",
"type": "@type",
"results": {
"@id": "onki:results",
"@container": "@list"
},
"prefLabel": "skos:prefLabel",
"altLabel": "skos:altLabel",
"hiddenLabel": "skos:hiddenLabel",
"@language": "FI"
},
"uri": "",
"results": [
{
"uri": "http://www.yso.fi/onto/yso/p22020",
"type": [
"skos:Concept",
"http://www.yso.fi/onto/yso-meta/Concept"
],
"localname": "p22020",
"prefLabel": "pyydystä ja päästä -kalastus",
"lang": "fi",
"altLabel": "catch and release -kalastus",
"vocab": "yso"
},
{
"uri": "http://www.yso.fi/onto/yso/p22337",
"type": [
"skos:Concept",
"http://www.yso.fi/onto/yso-meta/Concept"
],
"localname": "p22337",
"prefLabel": "CATCH-22",
"lang": "fi",
"vocab": "yso"
}
If anyone has insights into what I might be doing incorrectly, your assistance would be greatly appreciated. Best regards, Victor