【发布时间】:2021-01-09 01:17:37
【问题描述】:
当我在 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
【问题讨论】:
-
您使用的是什么版本的节点? (你可以通过
node --version找到这个) -
你安装了node-fetch吗?
-
v12.18.1。尝试了@3limin4t0r 的建议,但没有成功。 -
@ciaodejan 安装后你需要这个库吗?如果你不这样做,你仍然不能使用
fetch。确保您拥有const fetch = require('node-fetch'),如Loading and configuring the module中所述 -
不是
fetch没有被识别,是await关键字。
标签: javascript node.js async-await fetch