错误 TS2339:属性“然后”类型“void”上不存在

发布于 2025-01-11 01:05:53 字数 1181 浏览 0 评论 0原文

我有一个 Angular 应用程序,只需调用一个服务即可将图像上传到 AWS S3。

上传服务应将 AWS 响应返回给调用者。

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

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

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

这是调用者:

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;
    });
}

请问我缺少什么?

I have an Angular app with a simple call to a service to upload an image to AWS S3.

The upload service should return the AWS response to the caller.

I need to follow up on the upload request in the caller.

In the caller, I'm getting this error:

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

This is the caller:

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

This is the service being called:

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;
    });
}

What am I missing, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮是我掰弯的 2025-01-18 01:05:53

uploadDataFile 不返回任何内容,特别是不返回 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);
});

uploadDataFile doesn't return anything, expecially not a Promise.

Modify the last set of lines like this, adding a 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;
});

Also, I'm pretty sure that you are catching the error wrong:

this.aws.uploadDataFile(data).then(res => {
  if (res) {
    console.log('aws file returned: ', res);
  }
}).catch(err => {
  console.log('error: ', err);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文