Javascript/typescript承诺返回两种类型?
因此,我运行以下代码:Javascript/typescript承诺返回两种类型?,javascript,amazon-web-services,typescript,Javascript,Amazon Web Services,Typescript,因此,我运行以下代码: this.db.getDocumentClient() .then(client => client.query(params).promise()) .then(data => { this.items = data.Items; }) .catch(err => logger.debug('error in refresh tasks', err)) .then(() => { this.refr
this.db.getDocumentClient()
.then(client => client.query(params).promise())
.then(data => { this.items = data.Items; })
.catch(err => logger.debug('error in refresh tasks', err))
.then(() => { this.refresher && this.refresher.complete() });
得到这个错误:
typescript: D:/Developer/scrum/myApp123/src/pages/tasks/tasks.ts, line: 51
Property 'query' does not exist on type 'void | DocumentClient'. Property 'query' does not exist on type
'void'.
“void | DocumentClient”类型究竟是什么?getDocumentClient如下所示:
getDocumentClient() {
return Auth.currentCredentials()
.then(credentials => new AWS.DynamoDB.DocumentClient({ credentials: credentials }))
.catch(err => logger.debug('error getting document client', err));
}
Promise
是getDocumentClient()
函数的返回类型。问题是在.catch
中,您没有重新抛出错误,函数将解析为Promise
。要修复此问题,请将方法更改为:
getDocumentClient() {
return Auth.currentCredentials()
.then(credentials => new AWS.DynamoDB.DocumentClient({ credentials: credentials }))
.catch(err => { logger.debug('error getting document client', err); throw err; });
}
这将导致主代码中的承诺失败并进入catch部分,而不是解析为
void
,然后获取运行时错误。似乎没有用于客户端对象(第二行)的query
方法或者getDocumentClient
失败,进入catch
promise块,不返回任何内容