错误 TS2339:属性“然后”类型“void”上不存在
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
uploadDataFile
不返回任何内容,特别是不返回Promise
。像这样修改最后一组行,添加
return
。另外,我很确定您错误地捕获了错误:
uploadDataFile
doesn't return anything, expecially not aPromise
.Modify the last set of lines like this, adding a
return
.Also, I'm pretty sure that you are catching the error wrong: