当我在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
await
async
除非在Node 14.3.0+上并使用ESM,否则不能在函数外部使用。将代码包装成一个类似的代码块(async () => { /* your code here /* })();
,您应该会很好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句