0

Hello Guys! Hope you are fine I am new to react. I have an endpoint in spring boot and it return me an array of arrays (according to my need). but the problem is when I try to display it on react, it gives me an undefined error.

My API data will look like this;

[
 [
   2,
   "fa ",
   23,
  "fajhfl",
  "2021-07-20T06:44:19.000+00:00"
 ],
 [
  4,
  "amdflk",
  35,
  "lafjoil",
  "2021-07-21T06:44:43.000+00:00"
 ],
 [
  5,
  "fasdf",
  88,
  "fajhfl",
  "2021-07-22T06:44:52.000+00:00"
 ],
 [
  6,
  "nm",
  98,
  "faldjf",
  "2021-07-23T06:45:04.000+00:00"
 ]

]

My spring boot code is correct with proper use of Cross-origin but for question clarification, I put the Item controller code.

@RestController
@RequestMapping("/items")
@CrossOrigin(origins = "http://localhost:3000", allowedHeaders = "*")
public class ItemController {

    @Autowired
    private ItemService itemService;

    // Get all Items
    @GetMapping("/getItems")
    ResponseEntity<Object> getItems(){
        Object allItems = itemService.getAllItems();
        if(allItems == null){
            return ResponseEntity.noContent().build();
        }else{
            return ResponseEntity.ok(allItems);
        }
    }
}

Now In react I try to fetch it give me an undefined error,

<tbody className={tableStyle.tableBody}>
                    {props.body.map((key) => {
                        props.body[key].map((i, v) => {
                            return (
                                <tr>
                                    <td key={i}>{v}</td>
                                </tr>
                            );
                        });
                })}

But seeing this answer, I get the same error

TypeError: props.body[key].map is not a function
(anonymous function)
D:/My programming/React JS/Stock Register Managment System/srms/src/Components/GeneralTable/Table.jsx:30
  27 | </thead>
  28 | <tbody className={tableStyle.tableBody}>
  29 |  {Object.keys(props.body).map((key) => {
> 30 |      props.body[key].map((i, v) => {
     | ^  31 |          return (
  32 |              <tr>
  33 |                  <td key={i}>{v}</td>

Thanks In Advance:)

2
  • You have array of arrays, use props.body.map instead of Object.keys(props.body).map Commented Jul 22, 2021 at 4:49
  • I already use but I want to fetch its data, not just the whole array. Commented Jul 22, 2021 at 4:53

2 Answers 2

1

Try something like below

const data = [
  [2, "fa ", 23, "fajhfl", "2021-07-20T06:44:19.000+00:00"],
  [4, "amdflk", 35, "lafjoil", "2021-07-21T06:44:43.000+00:00"],
  [5, "fasdf", 88, "fajhfl", "2021-07-22T06:44:52.000+00:00"],
  [6, "nm", 98, "faldjf", "2021-07-23T06:45:04.000+00:00"],
];

const Component = (props) => {

  if (!props.data || 
    !Array.isArray(props.data) || 
    props.data.length < 1 || 
    !Array.isArray(props.data[0])) {
   return <div> No results </div>
  }

  return (
    <table>
      {props.data.map((row) => (
        <tr>
          {row.map((col) => (
            <td key={col}>{col}</td>
          ))}
        </tr>
      ))}
    </table>
  );
};

ReactDOM.render(<Component data={data} />, document.getElementById("app"))

ReactDOM.render(<Component data={[]} />, document.getElementById("app2"))

ReactDOM.render(<Component data={['some']} />, document.getElementById("app3"))
<script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>


<div id="app"></div>

<div id="app2"></div>

<div id="app3"></div>

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

3 Comments

When I run on time its correct all data is shown correctly but when I run it secondly it give me the same error i.e. row.map is not a function
Most likely that, next time data is not coming as array of array. Before return, check for condition if data is not array or array show the empty div.
@FeezanKhattak, Updated the answer. Check the snippet.
1

That's because you are trying to access key from an anonymous. You can directly use key.map() instead of an anonymous find > props.body[key]

Please see below:

<tbody className={tableStyle.tableBody}>
                        {props.body.map((key) => {
                            key.map((i, v) => {
                                return (
                                    <tr>
                                        <td key={i}>{v}</td>
                                    </tr>
                                );
                            });
                    })}

1 Comment

When I run on time its correct all data is shown correctly but when I run it secondly it gives me the same error i.e. row.map is not a function

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.