Ravenous - total from fetch API

Link to project: https://github.com/jkey774/codecademy-ravenous

I have been trying for a few days now to add a new feature to my ravenous app that displays a results total for number of businesses retrieved. I can console.log(jsonResponse.total) just before mapping each business but am unsure how to set this up to where it can be accessed in App.js to do something like:

in Yelp.js

const Yelp = {
  search(term, location, sortBy) {
    const limit = 21;
    return fetch(`https://cors-anywhere.herokuapp.com/https://api.yelp.com/v3/businesses/search?term=${term}&location=${location}&sort_by=${sortBy}&limit=${limit}`, {
      headers: {
        Authorization: `Bearer ${apiKey}`
      }
    }).then(function (response) {
      return response.json();
    }).then(function (jsonResponse) {
      if (jsonResponse.businesses) {
        console.log(jsonResponse.total);
        return jsonResponse.total;
      }
    }).then(function (jsonResponse) {
      if (jsonResponse.businesses) {
        return jsonResponse.businesses.map(function (business) {
          return {
            id: business.id,
            imageSrc: business.image_url,
            name: business.name,
            address: business.location.address1,
            city: business.location.city,
            state: business.location.state,
            zipCode: business.location.zip_code,
            category: business.categories[0].title,
            rating: business.rating,
            reviewCount: business.review_count
          };
        });
      }
    });
  }
};

and in App.js

searchYelp(term, location, sortBy) {
    Yelp.search(term, location, sortBy).then((total, businesses) => {
      this.setState({
        total: total,
        businesses: businesses
      })
    })
  }

How might this look?

For anyone who may be wondering, the solution was as follows:

.then(function(jsonResponse) {
        if (jsonResponse.businesses) {
          return {
            total: jsonResponse.total,
            businessList: jsonResponse.businesses.map(function(business) {
              return {
                id: business.id,
                imageSrc: business.image_url,
                name: business.name,
                address: business.location.address1,
                city: business.location.city,
                state: business.location.state,
                zipCode: business.location.zip_code,
                category: business.categories[0].title,
                rating: business.rating,
                reviewCount: business.review_count
              };
            })
          };
        }
      });
searchYelp(term, location, sortBy) {
    Yelp.search(term, location, sortBy).then(businessData => {
      this.setState({
        businesses: businessData.businessList,
        total: businessData.total
      });
    });
  }
2 Likes