-2

Hello i have a problem with passing the formdata from angular to Nestjs. I tried to pass the values to nestjs but it didn't work. I want to mail the formdata to my email.

this is angular

sendEmail() {
    const formData: any = new FormData();
    formData.append('naam', this.contactFormGroup.value.naam);
    formData.append('email', this.contactFormGroup.value.email);
    formData.append('bericht', this.contactFormGroup.value.bericht);
    console.log(formData);
    this.contactService.sendMail(formData);
  }

this is nestjs

@Post('sendmail')
  sendMail(@Body() formdataDto: FormDataDto) {
    return this.mailService.sendMail(
      formdataDto.naam,
      formdataDto.email,
      formdataDto.bericht,
    );
  }
Samir
  • 15
  • 5
  • How did it not work? Did you get errors? `undefined` values? Something else? Seeing the network request may be helpful here. – Jay McDoniel Aug 15 '22 at 17:54
  • @JayMcDoniel yes undefined values in my email. – Samir Aug 15 '22 at 18:13
  • If you're sending form data, do you have a form data parser in use? Nest by default parsesd application/json and application/x-www-form-urlencoded – Jay McDoniel Aug 15 '22 at 18:19
  • @JayMcDoniel if you mean in Angular I don’t use a form data parser. But when I send the mail I see on the network console that payload send the 3 values: name, email and message – Samir Aug 15 '22 at 18:41
  • No, on the server do you have a form data parser? – Jay McDoniel Aug 15 '22 at 18:42
  • No I don’t have a form data parser. I am new with nestjs , but do I need a form data parser in the main.ts. Can you provide me the code. – Samir Aug 15 '22 at 18:52
  • If you're sending form data, yes you need a form data parser. Follow the [`File Upload Docs`](https://docs.nestjs.com/techniques/file-upload) for how to bind the `FileInterceptor` and choose the right interceptor for you – Jay McDoniel Aug 15 '22 at 19:09

1 Answers1

0

The form data parser solved my problem.

@Post('sendmail')
  @UseInterceptors(FileInterceptor('formdata'))
  sendMail(@Body() formdataDto: FormDataDto) {
    return this.mailService.sendMail(
      formdataDto.naam,
      formdataDto.email,
      formdataDto.bericht,
    );
Samir
  • 15
  • 5