Undefined value after RxJs mapping Code Answer

Why am I getting undefined inside subscribe method? I am trying to combine to data sources. Customers and Categories. I am trying to fill Category property of customer based on matching CategoryId from Category collection.


constructor(private http: HttpClient) {}
  ngOnInit() {
    const custurmersWithCategory$ = combineLatest([
       map(([customers, categories]) => {
          customer =>
              Category: categories.find(
                c => customer.CategoryId === c.CategoryId
            } as ICustomer)

    custurmersWithCategory$.subscribe(data => {
      console.log(data);// why is this undefined?


You aren’t currently returning anything from map, thus you get undefined. As explained here: https://stackoverflow.com/a/41743119/6294072 your code translates to something like:

function (foo) { bar; }

which doesn’t return anything at all. So add

return customers.map( ...

and it will actually return some values.


As per comment if not wanting to add the return, you can just remove the curly brackets from inside map.


As a further comment, like mentioned in the answer I linked, this has nothing to do with rxjs map, but rather how arrow functions work 🙂

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji