我正在使用Node和Express构建一个服务器。在我的一个GET路径中,我有一个异步函数,它可以很好地返回我想要的数据,但是当我试图将这些数据添加到一个对象中,以便返回该路径所需的所有信息时,数据变成了这样:[ [ [Object], [Object] ], [ [Object] ] ]我如何防止充满数据的对象变成' object '?
参数productData是一个包含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)
}
...以下是控制台中显示的内容:
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] ] ]
}编辑:我通过移除我的套子级别修复了这个问题。我不需要我的对象按数组分组,所以我去掉了中级数组。
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)下面是控制台中显示的内容:
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
}
]
}发布于 2021-08-06 01:14:33
Node不会不加区别地记录对象,因为这可能会占用整个滚动缓冲区。如果对象有几层深,那么在某些情况下,它会记录占位符指示符,除非您显式地记录对象的字符串表示,而不是对象本身。
如果希望看到以JSON格式记录的完整对象,那么可以使用console.log(JSON.stringify(newProductData), false, 2)显式地实现这一点。
https://stackoverflow.com/questions/68674679
复制相似问题