3

So I make a call to a API and use it to setState.

My state:

  state = {
   candlesticks: []
};

My API call and promise function:

componentDidMount() {
    axios
      .get(
    "apiurl"
      )
      .then(data => {
        let mappedData = data.map((record) => {record.date *= 1000}); //getting the error here with the map()function
        this.setState({
          candlesticks: mappedData
        });
      });
  }

I've tried different variations on the code but it still gives this error. I'm using Expo framework.

undefined is not a function (evaluating 'data.map(function (record){record.date *= 1000})')

2
  • 1
    Probably you have to parse the data from the response. That could be something like data.body.map or data.content.map. Print data to see what it really contains! Commented May 25, 2017 at 12:59
  • I have used this same data on the web before so I know. However I used jQuery then. Commented May 26, 2017 at 9:05

1 Answer 1

2

Axios returns a response object. Inside the response you have a data property with your data.
So what you need to do is:

componentDidMount() {
    axios.get("apiurl")
    .then(response => {
        let mappedData = response.data.map((record) => {record.date *= 1000}); //getting the error here with the map()function
        this.setState({
            candlesticks: mappedData
        });
    });
}

Edit

Doesn't seem possible that there's a problem with axios, but just to rule it out, try to use the built-in fetch module and see if the data is still null.

componentDidMount() {
    fetch("apiurl")
    .then(response => {
        return response.json();
     })
    .then(data => {
        let mappedData = data.map((record) => {record.date *= 1000});
        this.setState({
            candlesticks: mappedData
        });
    });
}

If it's null then the problem is in your server.

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

9 Comments

I was aware of the response object this was a silly error. thank you.
The data returned from the response object is all null though.
How do I fix this?
This same map method and data worked when I was using with jQuery on the web before.
The data is returned correctly when the map() method isn't applied.
|

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.