I am attempting to create an array of intricate objects from the following data:
My goal is to utilize this object array to generate components using map()
- To structure the response type, I utilized :
// ... other types like Tag
export type DatasetInfoResult = {
license_title: string
relationships_as_object: string[]
maintainer_email: string
tags: Tag[]
//... all the params
}
export type DatasetInfoResponse = {
help: string
success: boolean
result: DatasetInfoResult
}
Here is a snippet of my code:
- datasetIDs is an array of strings containing dataset names such as sample-dataset-1
- getDataset fetches the API address and works correctly
export async function getAllDatasets() {
const datasetsIDs = (await listDatasets())
let allDatasets: DatasetInfoResult[]
datasetsIDs.map( async id => {
allDatasets.push( await getDataset(id) )
})
return allDatasets
Error message: TypeError: Cannot read properties of undefined (reading 'push')
Exploring an alternative approach:
export async function getAllDatasets() {
const datasetsIDs = (await listDatasets())
let allDatasets: DatasetInfoResult[]
for (let i = 0; i <= datasetsIDs.length; i++ ) {
const resp = ( await getDataset( datasetsIDs[i]) )
allDatasets = [...allDatasets, resp]
// allDatasets.push(resp)
}
return allDatasets
Error message: allDatasets is not iterable
What is the best way to achieve my objective of retrieving and utilizing multiple data sets?