【发布时间】:2021-12-31 00:31:06
【问题描述】:
我不断收到语法错误“Uncaught (in promise) ReferenceError: showQuestion is not defined”试图找到解决此问题的方法。而且我对整个事情还是很陌生。
fetch(
'https://jservice.kenzie.academy/api/random-clue?valid=true'
)
.then(response => response.json())
.then(randomDataId => {
const categoryDataId = randomDataId.categoryId;
console.log(`category id is: ${categoryDataId}`);
fetch(
`https://jservice.kenzie.academy/api/clues?category=${categoryDataId}`
)
.then(response => response.json())
.then(randomCatyId => {
const rightAnswer =
randomCatyId.clues[randomData].answer;
const showTitle =
randomCatyId.clues[randomData].category.title;
const showQuestion =
randomCatyId.clues[randomData].question;
showCategory.innerText = `Category: ${showTitle}`;
h2Question.innerText = `Question: ${showQuestion}`;
console.log('question: ' + showQuestion);
console.log('answer: ' + rightAnswer);
questionFetch();
});
});
function questionFetch() {
h2Question.innerHTML = `Question: ${showQuestion}`;
}
【问题讨论】:
-
很难从引用的代码中看出,但似乎 questionFetch 函数中对 showQuestion 的引用超出了定义范围。
标签: javascript fetch-api