Home > Software design >  error TS2339: Property 'then' does not exist on type 'void'
error TS2339: Property 'then' does not exist on type 'void'

Time:03-02

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?

CodePudding user response:

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