首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >异步函数返回转换为‘[Object]’的对象

异步函数返回转换为‘[Object]’的对象
EN

Stack Overflow用户
提问于 2021-08-05 23:59:14
回答 1查看 25关注 0票数 0

我正在使用Node和Express构建一个服务器。在我的一个GET路径中,我有一个异步函数,它可以很好地返回我想要的数据,但是当我试图将这些数据添加到一个对象中,以便返回该路径所需的所有信息时,数据变成了这样:[ [ [Object], [Object] ], [ [Object] ] ]我如何防止充满数据的对象变成' object '?

参数productData是一个包含prodNotArray的对象。

代码语言:javascript
复制
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)
         }
      ...

以下是控制台中显示的内容:

代码语言:javascript
复制
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] ] ]
}

编辑:我通过移除我的套子级别修复了这个问题。我不需要我的对象按数组分组,所以我去掉了中级数组。

代码语言:javascript
复制
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)

下面是控制台中显示的内容:

代码语言:javascript
复制
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
    }
  ]
}
EN

回答 1

Stack Overflow用户

发布于 2021-08-06 01:14:33

Node不会不加区别地记录对象,因为这可能会占用整个滚动缓冲区。如果对象有几层深,那么在某些情况下,它会记录占位符指示符,除非您显式地记录对象的字符串表示,而不是对象本身。

如果希望看到以JSON格式记录的完整对象,那么可以使用console.log(JSON.stringify(newProductData), false, 2)显式地实现这一点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68674679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档