0

I am learning how to upload files with nestjs. Specifically if my submission form has a title and an image file. I want the photo to be saved after I check some conditions. I have created an api to upload then use HttpService but it doesn't seem to work.

This is the upload controller:

@Post('/image')
  @UseGuards(AdminAuthGuard)
  @ApiOperation({ summary: 'Upload image Api ' })
  @UseInterceptors(FileInterceptor('file', optionsImage))
  async upload(@UploadedFile('file') file): Promise<{ filename: any }> {
    return { filename: `/image/${file.filename}` };
  }

I tried like this, but it doesn't work:

const formData = new FormData();
formData.append('file', file)
await this.http.post('http://localhost:3000/upload/image', formData)

1 Answers1

0

Nest's HttpService returns an Observable which is by default not-awaitable (it's not a Promise). It can be turned into a promise by being wrapped with lastValueFrom (from the rxjs package) or you can use rxjs operators to handle the response and return the Observable. Lots of extra information here and here

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147