I encountered an issue when attempting to filter an array of values from an observable:
The error message I received was: 'Type 'Observable' must have a 'Symbol.iterator' method that returns an iterator'
Here is the code snippet:
export class ItemsService {
orderItems: OrderItem[] = [];
orderItemsUrl = 'http://localhost:5000/order-items/';
getOrderItemsFromHttp(selectedOrderNumber): Observable<OrderItem[]> {
const tempArr = [];
const orderItems = of (this.http.get<OrderItem[]>(`${this.orderItemsUrl}`)
.subscribe(res => {
this.orderItems = res;
}) );
for (const orderItem of orderItems) { <--- The error occurs here
if (orderItem.orderNumber === selectedOrderNumber) {
tempArr.push(orderItem);
}
}
return of(tempArr);
}
}
By changing the following:
for (const orderItem of orderItems) {
to
for (const orderItem of [orderItems]) {
The error disappears, but the property orderNumber is no longer recognized. IntelliSense then displays:
Property 'orderNumber' does not exist on type 'Subscription'
What is the best way to fix this issue?