JavaScript Getting data from one axios to another in componentDidMount getting data from api,getting data from firestore,getting data

In componentDidMount I'm trying to get data called topic from /api/topics/${params.TopicId route then from response I want to send only topic.user_id to another route and get whole user as response. But this isn't working because they are sending request in the same time so the state of topic.user_id is empty. Is it a way that I can take piece of response and give it to the another request ? I'm doing it in componentDidMount so it will be done before component renders.

componentDidMount() {
    const {
      match: { params }
    } = this.props;

    axios
      .get(`/api/topics/${params.TopicId}`)
      .then(response => {
        this.setState({ topic: response.data });
      })
      .catch(function(error) {
        console.log(error);
      });

    axios
      .get(`/api/users/${this.state.topic.user_id}`)
      .then(response => {
        this.setState({ user: response.data });
      })
      .catch(function(error) {
        console.log(error);
      });
  }
Answer:1

You can chain your Promises together like this:

componentDidMount() {
  const {
    match: { params }
  } = this.props;

  axios
    .get(`/api/topics/${params.TopicId}`)
    .then(response => {
      this.setState({ topic: response.data });
      return axios.get(`/api/users/${response.data.user_id}`);
    })
    .then(response => {
      this.setState({ user: response.data });
    })
    .catch(function(error) {
      console.log(error);
    });
}
Answer:2
componentDidMount() {
  const {
    match: { params }
  } = this.props;

  fetch(`/api/topics/${params.TopicId}`,(response)=>{
    this.setState({ topic: response.data } , 
      {
        fetch(`/api/users/${this.state.topic.user_id}` ,(response)=>{
          this.setState({ user: response.data });
        } )
      });

}

const fetch = (uri,callBack) =>{
  axios
  .get(uri)
  .then(response => {
    callBack(response)
  })
  .catch(function(error) {
    console.log(error);
  });
}

it will be better to use the set state callback parameter

setState(updater[, callback])

https://reactjs.org/docs/react-component.html#setstate

because you need the state been updated before the next fetch

Answer:3

These are the geometries: http://p5js.org/examples/examples/3D_Geometries.php I've already tried the syntax for the 2d shapes but it doesn't appear to work. Is there a special syntax for the WEBGL ...

These are the geometries: http://p5js.org/examples/examples/3D_Geometries.php I've already tried the syntax for the 2d shapes but it doesn't appear to work. Is there a special syntax for the WEBGL ...

Today I wrote code for some programming contest. When I run it I was surprised because of error. Cannot read property 'forEach' of undefined in a place where looks like "error free space". sum = 0 ...

Today I wrote code for some programming contest. When I run it I was surprised because of error. Cannot read property 'forEach' of undefined in a place where looks like "error free space". sum = 0 ...

WHAT I WANT TO DO I want to shorten my code. This Drum Play App plays sound by pressing certain keys or clicking with your mouse. It works, but the code for click events is too long because I ...

WHAT I WANT TO DO I want to shorten my code. This Drum Play App plays sound by pressing certain keys or clicking with your mouse. It works, but the code for click events is too long because I ...

  1. short code with
  2. short code with twilio
  3. shorten code in python
  4. shorten code in excel

I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...

I want to make sure that I can't push a duplicated value into an array in a React state. The duplicated value is still going in the array though. I have tried using .includes but it is not working. ...

  1. how to include duplicate values in pivot table
  2. how to include duplicate values in sql