我正在执行 Stripes 集成步骤,并且在步骤 2.1 中发现我的代码出现错误 (https://stripe.com/docs/connect/collect-then-transfer-guide#create-an-account-link )
如何修复此错误?
代码:
const stripe = require('stripe')('someID');
const account = await stripe.accounts.create({
type: 'express',
});
错误:
仅当“模块”时才允许顶级“等待”表达式 选项设置为“esnext”或“system”,并且设置“target”选项 到“es2017”或更高版本.ts(1378)
您可以将 const 帐户的代码包装在异步函数中,因为您的目标选项不支持顶级等待。
const account = async () => {
await stripe.accounts.create({
type: "express",
});
};
这取决于你的代码是否想要返回某些东西或者你想在await之后执行一些其他任务。
如果您想使用顶级等待,有关使用顶级等待的更多信息请参见https://stackoverflow.com/a/56590390/9423152
这只是问题的解决方法,而不是其他用户提到的确切解决方案。 此外, 如果您在节点上使用 Typescript,您可以尝试更改 tsconfig 文件中的模块选项和目标。
这是您可能错过的东西:
当提供要编译的文件名时,会忽略tsc
中的配置。tsconfig.json
--help
中也提到了这一点,但我确实同意这有点不直观:
$ npx tsc --help
tsc: The TypeScript Compiler - Version 4.6.2
TS
COMMON COMMANDS
....
tsc app.ts util.ts
Ignoring tsconfig.json, compiles the specified files with default compiler options.
ts
文件并使用命令行参数提供正确的选项:所以你需要使用:
npx tsc -t es2022 -m es2022 --moduleResolution node --outDir dist src/runme.mts
tsc
指定 .ts 文件,使用 src
中的
tsconfig.json
这是一个包含顶级等待的正确设置的配置:
{
// https://www.typescriptlang.org/tsconfig#compilerOptions
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es2020"],
"module": "es2022",
"preserveConstEnums": true,
"moduleResolution": "node",
"strict": true,
"sourceMap": true,
"target": "es2022",
"types": ["node"],
"outDir": "dist"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
确保 tsconfig.json 中的
include
文件夹包含使用顶级等待的打字稿:
npx tsc
生成dist/runme.mjs
并且我编译的应用程序运行。
您可以只使用示例承诺。它会起作用的。
new Promise(async(resolve, reject) => { const res = await resPromise })