英文:
Data variable from API fetch function not working when declared
问题
我真的对js很陌生,但我已经不得不使用API并使用async/await来工作,所以我知道可能有些地方我不太明白...
这是我的代码:
async function collectWorks() {
const response = await fetch("http://localhost:5876/api/works");
const worksData = await response.json();
return worksData
}
async function testfunction() {
const worksData = await collectWorks();
let test = worksData[1].title;
console.log(test)
}
testfunction()
这是我的问题:我有一个用于获取API的异步函数。我应该在const worksData中获取数据,对吗?但当我尝试在testfunction中使用它时,它不起作用,我在控制台中得到以下错误:
Uncaught (in promise) ReferenceError: worksData is not defined at testfunction
然而,这就是我不理解的地方,当我去掉const前面的时候,它就可以正常工作,我确实得到了我需要的结果...我错过了什么?我不应该声明它吗?
(实际上,我之前注意到这一点,因为起初我忘记了添加const,然后继续进行项目,因为它运行得很好。当我注意到后,我尝试把它添加回来,但一切都崩溃了。最后我不得不重新开始,因为我认为代码的其余部分可能会对它产生影响,但现在我甚至无法开始,因为我卡在这个问题上了...我也尝试在fetch函数之前声明它。)
英文:
I'm really new to js and I already have to work with APIs and use async/await so I know there might be something I don't get here...
Here's my code:
async function collectWorks() {
const response = await fetch ("http://localhost:5876/api/works");
const worksData = await response.json();
return worksData
}
async function testfunction() {
await collectWorks();
let test = worksData[1].title;
console.log(test)
}
testfunction()
Here's my problem: I have my async function to fetch my API. I should get the data in my const worksData, correct? But when I try to use it in my testfunction it doesn't work and I get this in the console :
Uncaught (in promise) ReferenceError: worksData is not defined at testfunction
However, and that's what I don't understand, when I get rid of the const in front of worksData it works perfectly and I do get the result I need... What did I miss? Am I not supposed to declare it?
(I actually noticed this because at first I forgot to add the const and went on with my project because it worked fine. When I noticed, I tried to add it back but it broke everything. I ended up starting anew because I thought rest of the code might be messing with it but now I can't even start since I'm stuck with this problem... I've tried declaring it before the fetch function too.)
答案1
得分: 1
不要有别的内容。
你没有使用函数的返回值:
await collectWorks();
如果你希望返回值存储在一个名为 worksData
(或者任何其他名字)的变量中,可以将该值存储在一个变量中:
let worksData = await collectWorks();
(就像你在 collectWorks
函数内部已经为 response
和 worksData
做的那样。)
当我去掉 worksData 前面的 const 时,它就能完美运行,我确实得到了我需要的结果
因为没有变量声明(只有赋值),它是一个 隐式全局变量。基本上它会成为 window
对象上的一个属性。一般最好避免这样做。
英文:
You're not using the return value of the function:
await collectWorks();
If you want the return value to be in a variable called worksData
(or called anything at all really), store that value in a variable:
let worksData = await collectWorks();
(Exactly as you already do for response
and worksData
within the collectWorks
function.)
> when I get rid of the const in front of worksData it works perfectly and I do get the result I need
Because without the variable declaration (only the assignment), it's an implicit global. Basically it becomes a property on the window
object. Which is best to avoid in general.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论