0

I'm currently running through this tutorial - https://appdividend.com/2018/11/11/react-crud-example-mern-stack-tutorial/#React_CRUD_Example

I've substituted in my own API and i've successfully managed to create new records. I'm now trying to view a list of all the records but getting the following error

Screenshot of error message

Here is the code (exact copy of tutorial code except API link)



    import React, { Component } from "react";
    import axios from "axios";
    import TableRow from "./TableRow";

    export default class Index extends Component {
      constructor(props) {
        super(props);
        this.state = { address: [] };
      }
      componentDidMount() {
        axios
          .get("http://localhost:6505/api/v1/Address")
          // .then(res => console.log(res))

          .then(response => {
            this.setState({ address: response.data });
          })
          .catch(function(error) {
            console.log(error);
          });
      }
      tabRow() {
        return this.state.address.map(function(object, i) {
          return ;
        });
      }

      render() {...}

If i add in the line that's commented out above '.then(res => console.log(res))', the error message no longer shows and the page renders but without any data - see screenshot below of page and console. I've highlighted the new error message and also highlighted where the data is that i want to display.

Any pointers are greatly appreciated :)

enter image description here

6
  • where is the tabRow being invoked? Commented Apr 26, 2019 at 11:03
  • 1
    you can do this.setState({address:response.data.results Commented Apr 26, 2019 at 11:04
  • @nivendha Hi, the tabRow function is called after "render(){ return(", within a <tbody> Commented Apr 26, 2019 at 11:11
  • @lovepreetsingh thanks, this did cross my mind but didn't seem to make any difference. I'll leave it in the code though, as i have a feeling i may have more than one issue. Commented Apr 26, 2019 at 11:15
  • you are trying to map something that does not exist. Yo can set a initialState address=[] Commented Apr 26, 2019 at 11:18

2 Answers 2

1

Your data is in response.data.results so change it to this and it will work.

Thanks

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

1 Comment

This seemed to fix the issue. Thanks
0

The .map function is only available on array.

It looks like data isn't in the format you are expecting.

can you try with consoling response.data or response.data.results.

Can you replace this.setState({ address: response.data }); with this.setState({ address: response.data.results });

3 Comments

Thanks, i've just updated code to .then(res => console.log(res.data.results)) and results showed in the console as expected.
Then can you replace this.setState({ address: response.data }); with this.setState({ address: response.data.results });
Oh i just removed the .then(res => console.log(res.data.results)) line of code and saved the page, and the data is now showing! Thank you so much

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.