0

I have two applications running at the same time. The first one is fetching APIs from vessel-finder and finding only a specific number of boats. The second application is the user interface used to visualize data from the fetch, specifically latitude and longitude of boats.

The problem I have is that the fetch seems to arrive correctly to the end point but that throws an error of TypeError: data is not iterable as also shown here on the terminal.

It seems that the problem might be the API fetch. Below the code I am using for that:

router.get('/hello', async function(req, res, next) {
    try {
        const { data } = await axios.get(
            'https://api.vesselfinder.com/vesselslist?userkey=KEY'
        );
        const [ metaData, ships ] = data;
        console.log(data);
        res.send(data);
    } catch (error) {
        res.send(error);
        console.log(error);
    }
});

module.exports = router;

While investigating what the problem might be, I came across this source which also uses axios to get the call to the API. I wonder now if the fetch I am making is basically incomplete and because of that, data is not iterable... Also from this post I was able to handle a missing Promise using a try-catch block that was preventing me from compiling.

Is the problem due to a missing axios.get('API call').then.setState instruction I am missing?, if so how could I fix that?

Thank you very much for pointing to the right direction for solving this problem.

4
  • It simply indicates that the data returned by the endpoint is not a list or a list-like object so you can't do const [metaData, ships] = data on it. Open the network tab of your developer toolbar and inspect the response of your endpoint. It is probably not a list. Commented Mar 11, 2020 at 13:18
  • Thanks for reading the question. this is the Network tab response. What should I look for? Commented Mar 11, 2020 at 13:22
  • Can you try to print the content of data to the console? What does it show? I probably is not what you think. Commented Mar 11, 2020 at 13:28
  • Ok I just console.log(data + 'This is the error') and this is the result of the terminal. Is that telling you something? Commented Mar 11, 2020 at 13:35

1 Answer 1

3

look out for this line in your code -

 const [ metaData, ships ] = data;

perform object destructuring either between two objects or between two arrays. You are trying it out between an array and an object, which is a flaw. For more details, check https://javascript.info/destructuring-assignment#object-destructuring

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

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.