我尝试使用 fetch 来访问其他文件中的数据,但它一直返回
ReferenceError: fetch is not defined
const getData = async () => {
const fetchedData = await fetch("./dino.json");
const data = await fetchedData.json();
return data.Dinos;
};
getData().then(data => {
console.log(data); // will print the array
}).catch(error => {
console.error(error);
});
我正在看这个fetch API但它没有指定我们是否需要安装任何东西
最佳答案
您可能正在使用 node
,其中未定义 fetch
,它仅在浏览器中默认可用。您可以将其安装为 npm
包 node-fetch .
关于javascript - 访问另一个文件时未定义 fetch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66757416/