-3

I'm in react Native, and I have a request that im making using axios, and the request is supposed to return something like this: Json data

I want to be able to save only the "Products" array in a variable. This is the snippet of my cod, but it's giving me a 'Possible unhandled promise rejection' and I dont understand why:

    const [shoesData, setShoesData] = useState([]);

    useEffect(() => {
    const getShoesData = async () => {
        await axios.get("https://stockx.com/api/browse?productCategory=sneakers&sort=release_date&releaseTime=gte-" + Date.now().toLocaleString() + "&order=ASC&country=FR")
            .then(response => {
                let products = response.data.map(x => {
                    return x.products;
                });
                setShoesData(products);
                console.log(products);
            })
    }
    getShoesData();
}, [])

Thanks in advance for your help.

Daniel
  • 5
  • 4
  • Does this answer your question? [React Native: Possible unhandled promise rejection](https://stackoverflow.com/questions/38842499/react-native-possible-unhandled-promise-rejection) – MrUpsidown Jun 05 '22 at 11:12
  • It does solves the error, but not ultimately not my main problem – Daniel Jun 05 '22 at 11:19
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 05 '22 at 15:11

1 Answers1

-1

Try and wrap your await instruction with a try catch. One possible error can be that the data you get from the response can, in some cases, not have a products field.

vladc
  • 150
  • 1
  • 9
  • Thanks. I tried and it solved the warning, but not yet my problem. How is it possible that The data comes empty in some cases ? When I test my request with Postman it works correctly. I need to specifically get that 'Products' array. – Daniel Jun 05 '22 at 10:40
  • There are a lot of reasons outside your control that can make your code throw an error. The API may have changed and can return an object with different fields, or may be completely down when you are making the request. That is why catching the error by showing an alert or toast that the items could not be fetched is to be preferred. – vladc Jun 05 '22 at 17:21
  • Indeed it was the API that was down due to captchas problems. Thanks for your help in solving the unhandled promise. – Daniel Jun 06 '22 at 10:11