While creating a user profile, I am encountering an issue where the FormData being generated for sending is empty despite all other fields having values. Below is the code snippet from cadastro.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { AlertController, ActionSheetController } from '@ionic/angular';
import { PictureSourceType, Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { CadastroService } from 'src/app/services/cadastro.service';
@Component({
selector: 'app-cadastro',
templateUrl: './cadastro.page.html',
styleUrls: ['./cadastro.page.scss'],
})
export class CadastroPage implements OnInit {
public registerForm: FormGroup
constructor(
private cadastroService: CadastroService,
private router: Router,
private formBuilder: FormBuilder,
private alertController: AlertController,
private ActionSheetController: ActionSheetController,
private camera: Camera,
) { }
ngOnInit() {
this.registerForm = this.formBuilder.group({
foto: [''],
nome: ['', Validators.required],
email: ['', [Validators.required, Validators.email]],
cpf: ['', Validators.required],
telefone: ['', Validators.required],
password: ['', Validators.required],
conf_pass: ['', Validators.required]
})
}
cadastrar() {
console.log(this.registerForm.get('nome').value)
const formData = new FormData();
formData.append('nome', this.registerForm.get('nome').value);
formData.append('email', this.registerForm.get('email').value);
formData.append('cpf', this.registerForm.get('cpf').value);
formData.append('telefone', this.registerForm.get('telefone').value);
formData.append('password', this.registerForm.get('password').value);
formData.append('file', 'testeFile');
console.log(formData);
this.cadastroService.cadastrar(formData).subscribe((res: any) => {
console.log(res);
}, error => {
console.log(error)
})
}
}
The returned value from the console log shows a FormData {}. However, when logging any field from registerForm, the value is displayed correctly. Can anyone suggest what might be causing this issue in constructing the request?
In my Laravel backend, the logFile of the request displays:
[2020-05-29 03:17:25] local.ALERT:
array (
'data' =>
array (
),
)