Aza*_*dra 2 javascript asynchronous node.js express async-await
我正在尝试使用 Node v14.8 中添加的新顶级 await。请参阅此处和此处了解更多信息。我没有在这里找到有关使用此新功能的问题的问题,但我希望这不是重复的。最接近的是@DanStarns 对这个问题的回答,但我也尝试过他的解决方案(他的回答是在 14.8 版本之前)。
我正在将我的 Axios 实例从我的控制器中提取到一个独立的服务中。在该服务文件中,我尝试实例化然后导出使用 Spotify API 中存储的授权标头创建的 Axios 实例。由于然后将其导入我的控制器(传递到构造函数然后导出new SpotifyApiController(SpotifyApiService))并且导入是同步的,因此我尝试使用新的顶级 await 功能在启动/导入时完全实例化它,但收到以下错误:
[nodemon] starting `babel-node bin/www.js --harmony-top-level-await --experimental-repl-await`
E:\projects\harmonic-mixing-companion\server\services\SpotifyApiService.mjs:117
var SpotifyApiService = await createApiService();
^^^^^
SyntaxError: await is only valid in async function
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Module._compile (E:\projects\harmonic-mixing-companion\server\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.newLoader [as .mjs] (E:\projects\harmonic-mixing-companion\server\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (E:\projects\harmonic-mixing-companion\server\controllers\/SpotifyApiController.js:1:1)
Run Code Online (Sandbox Code Playgroud)
从错误来看,看起来 babel-node 自从const SpotifyApiService = ...成为var SpotifyApiService = .... 我也添加"@babel/plugin-syntax-top-level-await"到我的babel.config.json.
该服务的文件扩展名为.mjs. 我也尝试过"type": "module"在我的服务器的 package.json 中进行设置,但这也没有结果。如果我错了,请纠正我,但将我的整个后端服务器设置为模块对我来说也不合适,因为对我来说它听起来不像是一个模块化单元(相对于可重用的SpotifyApiService)。
使用console.log(process.version);我的主条目文件顶部的双重检查我的节点版本,它打印了预期的 14.14.0 版本。
SpotifyApiController.js 片段:
import SpotifyApiService from '../services/SpotifyApiService.mjs';
import handleHttpError from '../handlers/handleHttpError';
class SpotifyApiController {
constructor(spotifyApiService) {
this.spotifyApi = spotifyApiService;
}
...
}
Run Code Online (Sandbox Code Playgroud)
SpotifyApiService.mjs 片段:
...
const createApiService = async () => {
const accessToken = await authorizeApp();
console.log(accessToken);
const authHeader = { Authorization: `Bearer ${accessToken}` };
const axiosInstance = axios.create({
baseURL: 'https://api.spotify.com/v1',
headers: authHeader,
});
createAuthRefreshInterceptor(axiosInstance, authorizeApp);
return axiosInstance;
};
const SpotifyApiService = await createApiService();
export default SpotifyApiService;
Run Code Online (Sandbox Code Playgroud)
package.json 依赖项:
"devDependencies": {
"@babel/cli": "~7.12.1",
"@babel/core": "~7.12.1",
"@babel/eslint-parser": "~7.12.1",
"@babel/node": "~7.12.1",
"@babel/plugin-proposal-class-properties": "~7.12.1",
"@babel/plugin-syntax-top-level-await": "~7.12.1",
"@babel/plugin-transform-arrow-functions": "~7.12.1",
"@babel/plugin-transform-runtime": "~7.12.1",
"@babel/preset-env": "~7.12.1",
"@babel/preset-react": "~7.12.1",
"babel-loader": "~8.1.0",
"nodemon": "~2.0.5"
},
"dependencies": {
"axios": "~0.20.0",
"axios-auth-refresh": "~3.0.0",
"cookie-parser": "~1.4.5",
"cors": "~2.8.5",
"debug": "~4.2.0",
"dotenv": "~8.2.0",
"express": "~4.17.1",
"querystring": "~0.2.0"
}
Run Code Online (Sandbox Code Playgroud)
服务器使用以下 npm 脚本启动: "dev-server": "nodemon --exec babel-node bin/www.js --ignore dist/"
您可以将其包装在 async 函数中:
var SpotifyApiService = (async()=> await createApiService())();
Run Code Online (Sandbox Code Playgroud)
在你可以使用than 之后,catch
| 归档时间: |
|
| 查看次数: |
2708 次 |
| 最近记录: |