当我在 devtools 的控制台中编写此内容时:
const response = await fetch("http://localhost:5001/api/products", {
headers: {"Content-Type":"application/json"}
});
const products = await response.json();
我能够成功获取数据。
但是当我在 JS 文件中使用相同的代码运行我的节点项目时,我收到以下错误:
Uncaught SyntaxError: Unexpected reserved word
答案 0 :(得分:0)
await
不能在 async
函数之外使用,除非您使用的是 Node 14.3.0+ 并使用 ESM。将代码包装在像 (async () => { /* your code here /* })();
这样的块中,您应该很高兴。