Home > Mobile >  use formData with ngrx
use formData with ngrx

Time:06-17

I use ngrx and try to submit my formData and this is my code :

formData = new FormData();

onSubmit() {
   this.formData.set('name', this.userForm.get('name')?.value);
   this.formData.set('description', this.userForm.get('description')?.value);
   this.formData.set('price', this.userForm.get('price')?.value);
   this.formData.set('category', this.userForm.get('category')?.value);
   this.formData.set('available', this.userForm.get('available')?.value);
    this.store.dispatch(AddItem({this.formData} ))
 
  }

when I try to dispatch addItem action I have an error : Argument of type '{ this: any; }' is not assignable to parameter of type '{ item: Item; }'.

and the addItem action code is this :

export const AddItem = createAction(
  ItemActionsNames.AddItems,
  props<{ item: Item }>()
);

so how I can solve this error ???

CodePudding user response:

You need to accomodate the payload you've specified {item: Item} so the dispatch will be

this.store.dispatch(AddItem({item: this.formData}))

It expects a key/value pair as you've specified in your props in the createAction function.

  • Related