【发布时间】:2020-08-10 05:15:30
【问题描述】:
节点 14.7.0,npm 6.14.7,打字稿 3.7.3。
我有一个查询 postgres 数据库的方法,它会根据参数返回它找到的第一行或查询的整个结果。类似这样:
async getRecord(options: {singleRow: boolean}): Promise<QueryResult> | Promise<Record> {
const result:QueryResult = await postgres.query(...);
if (options && options.singlerow) {
return result.rows[0];
}
return result;
}
但是,我收到以下错误:The return type of an async function or method must be the global Promise<T> type. 当我删除 | Promise<Record> 部分时,我没有收到错误,即使第一个返回(在 if 语句中)返回 Promise<Record>。我应该怎么做才能解决这个问题?
【问题讨论】:
-
尝试将您的回报包装成 Promise,例如
Promise.resolve(result.rows[0])
标签: node.js typescript async-await