0

I'm trying to map but I don't manage to map orders here. It's written cannot read property of undefined but products work.

This is my class:

const p = products.map(p => {
  return (
    <Product
    key={p._id}
    product={p}
    />
  );
});
const o = orders.map(o => {
  return (
    <div
    key={o._id}
    product={p}
    />
  );
});

This is the console log of the object:

console log of the fetched data that I want to map

4
  • Can you please share console.log of orders. Commented Mar 15, 2019 at 23:16
  • Where does orders come from? The second item in the array you posted? Commented Mar 15, 2019 at 23:17
  • 1
    What is the exact error you are getting? Commented Mar 15, 2019 at 23:19
  • 1
    There is an array inside an array. You expect there to be a single object in each item in your array. Commented Mar 15, 2019 at 23:24

2 Answers 2

2

In second map you should change product={p} to product={o}. P is unknown variable there.

const o = orders.map(o => {
  return (
    <div
    key={o._id}
    product={o}
    />
  );
});
Sign up to request clarification or add additional context in comments.

Comments

0

As @IfSoGirl mentioned you need to supply o but not p to product prop in second map method

Further to that, here is your simplified map functions. Since it’s single jsx element under map so no need of return syntax

   const p = products.map(p => <Product key={p._id} product={p}/>);

   const o = orders.map(o => <div key={o._id} product={o}/>);

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.