2

I am attempting to fetch multiple URLs in React using the Promise.all() method:

const urls = [
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=2",
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=3",
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=4"
];

Promise.all(urls.map(url => fetch(url).then(res => res.json()))).then(
  members => {
    console.log(members);
    this.setState({
      nearbymems: members.members
    });
  }
);

The console.log is returning all of the URLs together but it is not displaying them on my map application. I have declared an empty array in my state : nearbymems: []

I will also post my render() function for displaying the data as markers in leaflet:

{this.state.nearbymems.map(members => (
  <Marker
    position={[members.location.latitude, members.location.longitude]}
    icon={myIcon1}
  >
    <Popup>
      <h1 className="lead">{members.name} </h1>

      <PopupModal initialModalState={true} />
    </Popup>
  </Marker>
))}

Thank you.

edit:

Output of array:

 (3) [{…}, {…}, {…}]
 0:
   format:"json"
   latlon:"53,-6"
   members: Array(50)
   0:
      bio:" "
      date:""
      id:44967
      location:
         {latitude: "53.31138992", longitude: "-6.24345493"}
      name: "______"
      url:"https://thesession.org/members/___"
 __proto__: Object
7
  • can you show the output of console.log(members);? because members will an array. Commented Mar 12, 2019 at 9:42
  • 1
    Why nearbymems: members.members? if members is an array, members.members is undefined. Commented Mar 12, 2019 at 9:43
  • 1
    Maybe you want to add an additional .then(res => res.members) to each promise, since members.members will be undefined? Commented Mar 12, 2019 at 9:44
  • @MayankShukla I have posted the output above. Commented Mar 12, 2019 at 14:47
  • 1
    Judging by the array in your image, you should just have to write this.setState({ nearbymems: members }); Commented Mar 12, 2019 at 14:55

2 Answers 2

2

You want to take the members array from each fetch response, and then concatenate these arrays into one before you put it as nearbymems in state.

Promise.all(
  urls.map(url =>
    fetch(url)
      .then(res => res.json())
      .then(res => res.members)
  )
).then(members => {
  this.setState({
    nearbymems: [].concat(...members)
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Promise.all returns an array with each item containing the resolved value of its respective promise. Therefore you cannot simply access members from the result of Promise.all. Instead you must merge all the values together to get one large array of each promise's members value.

This will return all members from each request into a single flattened array.

const urls = [
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=2",
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=3",
  "https://thesession.org/members/nearby?latlon=53,-6&radius=1000&format=json&perpage=50&page=4"
];
const requests = urls.map(url => fetch(url).then(res => res.json()));
const toMembers = responses => responses.map(response => response.members);

Promise.all(requests).then(toMembers).then(members => console.log(members.flat()));

Comments

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.