0

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
    }
  ]
}
2
  • Thank you, @Mike'Pomax'Kamermans! That fixed my problem. Commented Aug 6, 2021 at 0:32
  • good to know, I've turned the comment into an answer so you can accept it. Commented Aug 6, 2021 at 1:15

1 Answer 1

1

Node does not indiscriminately log objects, because that could take up your entire scroll buffer. If objects are several levels deep, at some point it'll log placeholder indicators unless you explicitly log the string representation of your objects, instead of the objects themselves.

If you wanted to see the full objects logged in JSON format, then you want to explicitly do that by using console.log(JSON.stringify(newProductData), false, 2).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.