0

I've got this code set and a json file called statistics.json but when I log the data it says data is not defined

Anyone knows how to solve this?

const showStatistics = function(){
    fetch("../assets/statistics.json")
    .then(res => res.json())
    .then(data => {data.stats})
    .catch(err => console.error(err));

    console.log(data);
}
2
  • 2
    There's a strange line in your code: {data.stats}. What is that supposed to do? Commented Dec 25, 2020 at 15:53
  • shows stats array of the data of the json Commented Dec 25, 2020 at 16:19

1 Answer 1

1

You need to log data inside callback at then, because it is asynchronous.

const showStatistics = function(){
    fetch("../assets/statistics.json")
    .then(res => res.json())
    .then(data => {
        console.log(data);
    })
    .catch(err => console.error(err));

    
}
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.