错误 TS2339:属性 'then' 在类型 'void' 上不存在

error TS2339: Property 'then' does not exist on type 'void'

我有一个 Angular 应用程序,可以简单地调用服务将图像上传到 AWS S3。

上传服务应该return AWS 对调用者的响应。

我需要跟进调用方的上传请求。

在调用方中,我收到此错误:

error TS2339: Property 'then' does not exist on type 'void'

这是来电者:

this.aws.uploadDataFile(data).then(res => {
    if (res) {
      console.log('aws file returned: ', res);
    }
  }, err => {
    console.log('error: ', err);
});

这是正在调用的服务:

uploadDataFile(data: any) {
    const contentType = data.type;
    const bucket = new S3({
      accessKeyId: environment.awsAccessKey,
      secretAccessKey: environment.awsSecret,
      region: environment.awsRegion
    });
    const params = {
      Bucket: environment.awsBucket,
      Key: data.name, //manipulate filename here before uploading
      Body: data.value,
      ContentEncoding: 'base64',
      ContentType: contentType
    };
    var putObjectPromise = bucket.putObject(params).promise();
    putObjectPromise.then(function(data) {
      console.log('succesfully uploaded the image! ' + JSON.stringify(data));
      return data;
    }).catch(function(err) {
      console.log(err);
      return err;
    });
}

请问我遗漏了什么?

uploadDataFile 没有 return 任何东西,特别是 Promise.

像这样修改最后一组行,添加一个return.

return putObjectPromise.then(function(data) {
  console.log('succesfully uploaded the image! ' + JSON.stringify(data));
  return data;
}).catch(function(err) {
  console.log(err);
  return err;
});

此外,我很确定您发现错误:

this.aws.uploadDataFile(data).then(res => {
  if (res) {
    console.log('aws file returned: ', res);
  }
}).catch(err => {
  console.log('error: ', err);
});