My api call returns data the following way:
{
"items": [
{
"id": 1,
"descp": "item 1",
},
{
"id": 2,
"descp": "item 2",
},
{
"id": 3,
"descp": "item 3",
}
]
}
I am trying to get the array of items from this using rxjs but have not been able to so far. I keep running into
core.js:4197 ERROR TypeError: You provided an invalid object where a stream was expected.
getItems() {
const iAmStuck$ = this.service.getAllItems();
iAmStuck$
.pipe(mergeMap((insideArray) => insideArray))
.subscribe((res) => console.log(res));
}
service call
getItems(): Observable<Item[]> {
const url = `${environment.url}/allItems`;
return this.http.get<Item[]>(url);
}
I created the following interfaces
export interface IInsideItems{
id: number;
descp: string;
}
export interface Item{
items: IInsideItems[];
}
In the end of all this, I want to put id and descp into a new array of IDropdown
IDropdown{
id: string;
descp: string;
}
getAllItemsfunction. Also, what is your desired outcome? Your comment in the code:<-- how to return a streamcan be answered just by saying "Usereturn", but I guess that's not exactly what you're wondering. Be specific.mergeMap? That's whats causing you problems.mergeMapand thenmapto get the data out but I guess that is not the right way