I'm building a server with Node and Express. Within one of my GET routes, I have an asynchronous function that returns the data I want just fine, but when I try to add that data to an object so that I can return all the information needed for the route, the data turns into this: [ [ [Object], [Object] ], [ [Object] ] ]
How do I prevent my objects full of data from turning into '[Object]'?
The parameter "productData" is an object which includes "prodNotArray".
getProductData()
.then(async productData => {
try {
const getNotCerts = productData.prodNotArray.map(async notion => {
try {
const notCerts = await NotionsService.getCertsForNot(
req.app.get('db'),
notion.id
)
return notCerts
} catch (e) {
console.log('catch getNotCerts error:', e)
next(e)
}
})
const awaitNotCerts = await Promise.all(getNotCerts)
console.log('awaitNotCerts', awaitNotCerts)
const newProductData = {
...productData,
notCertArray: [
...awaitNotCerts
]
}
console.log('newProductData', newProductData)
return newProductData
} catch (e) {
console.log('catch getProductData().then() error:', e)
next(e)
}
...
Here's what shows up in the console:
awaitNotCerts [
[
{
notion_id: 5,
id: 1,
english_name: 'Global Organic Textile Standard',
website: 'https://www.global-standard.org/',
approved_by_admin: true,
date_published: 2021-07-16T22:58:20.803Z
},
{
notion_id: 5,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
}
],
[
{
notion_id: 6,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
}
]
]
newProductData {
notCertArray: [ [ [Object], [Object] ], [ [Object] ] ]
}
Edit: I fixed the problem by removing my nest levels. I didn't need my objects to be grouped by arrays, so I got rid of the mid-level arrays.
const unnestNotCerts = () => {
const unnestedNotCerts = []
awaitNotCerts.forEach(array => {
array.forEach(object => {
unnestedNotCerts.push(object)
})
})
return unnestedNotCerts
}
console.log('unnestNotCerts()', unnestNotCerts())
const newProductData = {
...productData,
notCertArray: unnestNotCerts()
}
console.log('newProductData', newProductData)
And here's what shows up in the console now:
unnestNotCerts() [
{
notion_id: 5,
id: 1,
english_name: 'Global Organic Textile Standard',
website: 'https://www.global-standard.org/',
approved_by_admin: true,
date_published: 2021-07-16T22:58:20.803Z
},
{
notion_id: 5,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
},
{
notion_id: 6,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
}
]
newProductData: {
notCertArray: [
{
notion_id: 5,
id: 1,
english_name: 'Global Organic Textile Standard',
website: 'https://www.global-standard.org/',
approved_by_admin: true,
date_published: 2021-07-16T22:58:20.803Z
},
{
notion_id: 5,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
},
{
notion_id: 6,
id: 3,
english_name: 'Organic',
website: 'organic.com',
approved_by_admin: false,
date_published: 2021-07-28T19:08:54.912Z
}
]
}