angularangular-materialakitaangular-akita

mat-autocomplete displaying always the same name with Akita


I have an issue with the mat-autocomplete of my form. When I write something in the field, it shows two times the last entry of my database with the same name :

example

database

I'm using Akita - State Management for Angular in my project and I cannot find the root cause of this issue. Maybe you can help me?

Here is my code :

assistants-page.component.html

<!-- Some code -->

<!-- Nationality -->
<ng-container *ngIf="!(loading$ | async); else loadingTpl">

  <mat-form-field>
    <input matInput type="text" placeholder="Nationalité" formControlName="nationality" [matAutocomplete]="auto">
    <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
      <mat-option *ngFor="let nationality of nationalities$ | async" [value]="nationality">
         {{ nationality.name }}
      </mat-option>
     </mat-autocomplete>
   </mat-form-field>

   </ng-container>

   <ng-template #loadingTpl>Loading...</ng-template>

assistants-page.component.ts

export class AssistantsPageComponent implements OnInit, OnDestroy {
  /* Some code */

  loading$: Observable<boolean>;
  nationalities$: Observable<Nationality[]>;

  constructor(
    private assistantsQuery: AssistantsQuery,
    private assistantsService: AssistantsService,
    private fb: FormBuilder,
    private nationalitiesQuery: NationalitiesQuery,
    private nationalitiesService: NationalitiesService,
  ) { }

  ngOnInit() {
    this.formGroup = this.fb.group({
      title: ['', Validators.required],
      lastName: ['', Validators.required],
      firstName: ['', Validators.required],
      nationality: null
    });

    this.nationalitiesService.get().subscribe();

    this.loading$ = this.nationalitiesQuery.selectLoading();

    this.nationalities$ = this.formGroup.get('nationality').valueChanges.pipe(
      switchMap(value => this.nationalitiesQuery.selectAll({
         filterBy: entity => entity.name.toLowerCase().includes(value)
      }))
    );

    this.persistForm = new PersistNgFormPlugin(this.assistantsQuery, createAssistant).setForm(this.formGroup);
  }

  displayFn(nationality?: Nationality): string | undefined {
    return nationality ? nationality.name : undefined;
  }

nationalities.service.ts

export class NationalitiesService {

/* Some code */

  get(): Observable<Nationality[]> {
    const request = this.nationalitiesDataService.get().pipe(
      tap(response => this.nationalitiesStore.set(response)
    ));

    return this.nationalitiesQuery.isPristine ? request : noop();
  }

nationalities-data.service.ts

export class NationalitiesDataService {

  /* Some code */

  get(): Observable<Nationality[]> {
    return this.http.get<Nationality[]>(this.url);
  }

Any input to solve this?

Thanks in advance for your help


Solution

  • The only reason that I can think of is that your id key is different than id. For example:

    entity = { id: 1, title: '' }
    

    You can define custom id key, like this:

    @StoreConfig( { name: '', idKey: 'todoId' } )
    class TodosStore {}
    
    entity = { todoId: 1, title: '' }
    

    Check out the docs.