Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/typescript承诺返回两种类型?_Javascript_Amazon Web Services_Typescript - Fatal编程技术网

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块,不返回任何内容