0

I have following code and I am trying to display the data from the object of objects. The error that I receive is TypeError: Cannot read properties of undefined. I understand that the data isn't in the format of 'array of objects', however I still don't know how to properly map it. I would appreciate some help here...

import Layout, { siteTitle } from '../components/layout';
import { useState, useEffect } from 'react'

export default function Home({ allPosts }) {  
  return (
    <Layout home>
      <Head>
        <title>{siteTitle}</title>
      </Head>
      <section>
        {Object.values(allPosts.bpi).map(({ section, idx }) => (
          <li className={utilStyles.listItem} key={idx}>
           {section.description}
          <br />
          {section.code}
          <br />
        </li>
        ))}
      </section>
    </Layout>
  );
}

export async function getStaticProps() {
  let data = [];
  let error = "";
  try {
    const res = await fetch(
      "https://api.coindesk.com/v1/bpi/currentprice.json",
    );
    data = await res.json();
  } catch (e) {
    error = e.toString();
  }

  return {
    props: {
      allPosts: data,
      error,
    },
  };
}

Logged data: enter image description here

Object looks like this

{
"chartName": "Bitcoin",
"bpi": {
      "USD": {
           "code": "USD",
           "symbol": "&#36;",
           "rate": "20,220.5728",
           "description": "United States Dollar",
           "rate_float": 20220.5728
      },
     "GBP": {
           "code": "GBP",
           "symbol": "&pound;",
           "rate": "16,896.1488",
           "description": "British Pound Sterling",
           "rate_float": 16896.1488
      },
   }
}

2 Answers 2

0

You don't access the USD and GBP objects. You are just getting their names. But you can access them by name like this:

<section>
    {Object.values(allPosts.bpi).map((section, idx) => (
        <li className={utilStyles.listItem} key={idx}>
           {allPosts.bpi[section].description}
            <br />
            {allPosts.bpi[section].code}
            <br />
        </li>
    ))}
</section>

EDIT

It should be section.description instead of allPosts.bpi[section].description. Same for the object code.

<section>
    {Object.values(allPosts.bpi).map((section, idx) => (
        <li className={utilStyles.listItem} key={idx}>
           {section.description}
            <br />
            {section.code}
            <br />
        </li>
    ))}
</section>
Sign up to request clarification or add additional context in comments.

10 Comments

I still get 'TypeError: Cannot read properties of undefined' error
Can you try logging data when you fetch it?
Also, on what line does that error occur?
I added the pic of logged data and the error occurs on the line {allPosts.bpi[section].description} or in my case {section.description}
Did you change (({ section, idx }) to ((section, idx)? You don't need to destructure it here.
|
0

You are using the map method wrong. Try this.

Object.values(allPosts.bpi).map((section, idx) => ....

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.