"Utilizing FormData in an IONIC 5 project with

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 (
  ),
)  

Answer №1

FormData is constantly changing and evolving. Though, its data remains hidden when using the console.log(formData) function. However, you can access and view the data by utilizing the following commands:

console.log(formData.get('name'));
console.log(formData.get('email'));

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unexpected Memory Drain in Ionic and Cordova on iOS

I've encountered an unusual memory leak in my Ionic and Cordova application. This leak is not present when running the app in Chrome, but it clearly appears when I test the app. The issue arises when I need to iterate through a large data set and assi ...

Is it possible to verify if an object matches a type without explicitly setting it as that type?

In order to illustrate my point, I believe the most effective method would be to provide a code snippet: type ObjectType = { [property: string]: any } const exampleObject: ObjectType = { key1: 1, key2: 'sample' } type ExampleType = typeof ...

Guide on inserting Angular 2 attributes within a variable in an Angular 2 TypeScript class

I have a variable 'content' in the 'findCharityHome.ts' typescript class structured like this. let content = ` <b>`+header+`</b> <button style='background-color:#428bca;color:white; ...

Could someone provide an explanation for the meaning of the phrase "class User extends Model<UserAttribute UserCreationAttribute>"?

View Image of the Issue I am puzzled by why we are utilizing both UserCreationAttribute and UserAttribute in that specific arrow, especially when UserCreationAttribute is created by omitting one field from UserAttribute. Can someone please clarify this fo ...

Setting up a global CSS and SASS stylesheet for webpack, TypeScript, Phaser, and Angular: A step-by-step guide

A manual configuration has been set up to accommodate all the technologies mentioned in the title (webpack, typescript, phaser, and angular). While it works perfectly for angular component stylesheets, there seems to be an issue with including a global st ...

Leveraging the spread operator in cases where the value is null

Is there a more elegant solution for handling null values in the spread operator without using if-else statements? In this specific case, I only want to spread assignedStudents if it is not undefined. When attempting to do this without using if-else, I e ...

Utilize JavaScript libraries in a TypeScript project

Looking to integrate a payment system called iyzico into my project. The iyzico payment library can be found here. However, there are no types available for it. How can I utilize this library in my Node.js TypeScript Express backend project? I attempted t ...

"Exploring the differences between normalization structures and observable entities in ngrx

I'm currently grappling with the concept of "entity arrays" in my ngrx Store. Let's say I have a collection of PlanDTO retrieved from my api server. Based on the research I've done, it seems necessary to set up a kind of "table" to store th ...

Using Pipe directive in Angular 2 with ES5: Error - Directive annotation missing from constructor

My simple pipe looks like this: app.DisplayKeystrokePipe = ng.core .Pipe({ name: "displayKeystroke" }) .Class({ transform: function() { } }); On the other hand, I have a more complex component/directive: app.Drop ...

What is the most effective way to retrieve a specific type of sibling property in TypeScript?

Consider the code snippet below: const useExample = (options: { Component: React.ComponentType props: React.ComponentProps<typeof options.Component> }) => { return } const Foo = (props: {bar: string; baz: number}) => <></& ...

TS2322 error: What does it mean when the type is both not assignable and assignable?

I've been delving into the world of generics with Java and C#, but TypeScript is throwing me for a loop. Can someone shed some light on this confusion? constructor FooAdapter(): FooAdapter Type 'FooAdapter' is not assignable to type 'A ...

Which option is more beneficial for intercepting API data in Angular 6: interfaces or classes?

My API returns JSON data that is not structured the way I need it, so I have to make changes. { "@odata.context":"xxxxxx", "id":"xxxxxxxx", "businessPhones":[ ], "displayName":"name", "givenName":"pseudo", "jobTitle":null, "ma ...

Error alert! A token error has been detected while executing Angular tests under <Jasmine>

I've been navigating the world of Angular testing, but I've hit a roadblock that I can't seem to bypass. Despite my efforts to consult the documentation and scour Google for answers, I remain stuck with a cryptic error message stating "Inval ...

Show only specific items in an AngularJS application

As a newcomer to AngularJS and the Ionic framework, I'm currently working with the basic Starter Tabs Ionic template. I would like to implement a "Favourite/Bookmark" feature for certain items and display them on a separate tab. The structure of my b ...

Error in TypeScript: It is not possible to use a component with MUI styling as a JSX element

I can't figure out what's going wrong. I'm attempting to include a child component in the main page, and I have a feeling it has something to do with MUI styles being applied at the end. I removed all unnecessary code and still encounter thi ...

Why is it not possible to declare an interface or type within a TypeScript class?

I am struggling to define interface | type within a TypeScript class. Here is the code snippet: class MyClass { interface IClass { name: string, id: string } } However, I keep encountering this error: Unexpected token. A constructo ...

Embedding an Iframe in Angular 2 directly from the database

Looking for assistance with iframes in Angular 2. Initially, embedding an iframe directly into a component's template functions correctly. <iframe src='http://plnkr.co/edit/zZ0BgJHvQl5CfrZZ5kzg?p=preview | safeUrl' allowtransp ...

The Mat-Datepicker will always show the current date in your local time zone, never in

Utilizing a datepicker and momentjs, I have successfully implemented sending UTC dates to my server. However, the issue arises when retrieving these UTC dates back into the date picker, as it always reflects my timezone (EST). This discrepancy can result i ...

What is the reason behind this build error I am encountering while using react-three-xr?

I'm having trouble understanding this error message. What steps can I take to resolve it? Although I have included three-xr in my react app, I am encountering the following error: Failed to compile. ../../node_modules/@react-three/xr/src/DefaultXRCon ...

Using Typescript to pass a property as one of the keys in an object's list of values

In my React Native project, I need to pass a string value from one component to another. The different options for the value can be found in the ScannerAction object: export const ScannerAction = { move: 'move', inventory: 'inventory&apo ...