0

I have the following data structure that I am trying to render in a table in React. However I keep getting a undefined issue getting the 'errors' nested array.

My data is the following:

const messages= [
  { invoice: "81", errors: [{ Message: "Invoice # must be unique." }] },
  { invoice: "82", errors: [{ Message: "Invoice # must be unique." },
                            { Message: "No total amount." }]},
  { invoice: "85", errors: [{ Message: "Invoice # must be unique." }] }
 ];

My React table is the following:

 <table>
  <thead>
   <tr>
    <th>Invoice</th>
    <th>Errors</th>
   </tr>
  </thead>
   {messages.map(e => {
    return (
     <tbody>
      <tr>
       <td>{e.invoice}</td>
        {messages.errors.map(e => {
         return (
          <td>
           <ul>{e.errors}</ul>
          </td>
          );
       })}
      </tr>
     </tbody>
     );
    })}
 </table>

My table is rendered and e.invoice is displaying corrrectly, however I am getting a "cannot map errors of undefined" error.

2 Answers 2

1

Its because your messages is not a javascript object, rather it's an array

You need to use

{e.errors.map(item => {
     return (
      <td>
       <ul>{item.Message}</ul>
      </td>
      );
})}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Worked perfectly!
1

{e.errors.map(error => { is the correct chain.

1 Comment

Thanks! Worked perfectly!

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.