I’m using the Advice Slip API. As the title says, when I input the JSON data into the variable, like this:
JavaScript
x
3
1
let advi;
2
fetch("https://api.adviceslip.com/advice").then(r => r.json()).then(adv => advi = adv);
3
It gives me the error that I mentioned. However, when I replace
.then(adv => advi = adv)
with
.then(console.log)
it gives me an object with the advice. However, since I don’t want to just console.log the advice as I need to use it in my website, I need to find a way to use it in a variable.
Advertisement
Answer
index.js
JavaScript
1
9
1
function showData() {
2
let advi;
3
fetch("https://api.adviceslip.com/advice").then(r => r.json()).then(adv => {
4
advi = adv;
5
console.log(advi);
6
})
7
}
8
9
showData();