Philippe Corrèges Philippe Corrèges - 1 year ago 59
TypeScript Question

How to access particular Observable items?

I have a function:

getCategories(): Observable<any>
{
return this.category.find({where: {clientId: this.userApi.getCurrentId()}})
};


where this.category.find return type is
Observable<T[]>
.

I call getCategories() with:

const source = this.categoryService.getCategories();
const example = source.map(Categ=> Categ.id);
const subscribe = example.subscribe(val => console.log(val));


and Categ.id is undefined

If I run:

const source = this.categoryService.getCategories();
const subscribe = source.subscribe(val => console.log(val));


directly, the output is :
enter image description here

I think I have a type problem somewhere but cannot figure it out.

Any idea ?

Thanks and Regards,

Answer Source

As an array of categories is returned by the getCategories() and not just a single category as is evidenced by your subscribe's console log.

You could try something like:

const example = source.map((categories) => categories.map((category) => {return category.id}));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download