#ReadonlyMap.get() returning undefined despite log showing that the element exists

4 messages · Page 1 of 1 (latest)

mellow dawn
#

I'm trying to access a value inside of a ReadonlyMap<string, object> and despite the key being there it returns undefined. Can anyone see why this is happening?

public getById(id: string): Observable<MapModel | null> {
  console.log('id', id)
  return this.maps$.pipe(
    tap((map: ReadonlyMap<string, MapModel>) => console.log('map', map)),
    map((map: ReadonlyMap<string, MapModel>) => map.get(id) ?? null),
    tap(a => console.log('result', a))
  );
}
``` ```ts
public ngOnInit(): void {
  this.map$ = this.route.params.pipe(
    map((params: Params): string => params['id']),
    tap(a => console.log('test', a)),
    switchMap((id: string) => this.dataService.getById(id))
  );
}
scarlet karma
#

Well, the logs indicate that the map is empty, so the result being logged is null. The a second map is being logged (because this.map$),emits a second time, and then it's not empty anymore.
And nothing being logged is undefined.

mellow dawn
#

The first time it gets logged as null is expected, the second "result" log is what confuses me, since there the map should exist and return the contents at '1'. At least that's what I expected.

scarlet karma
#

The key probably is number rather than a string. Add more debug logs (or use your debugger) to find out.