Unable to locate a differ supporting element '[object Object]' that is categorized as 'object'

My goal is to display the name property, but store it as the value of _id for the mat-select control. I also want to save the selected option in the selectedIngridient variable.

However, I encountered the following error message:

"Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."

Despite researching similar issues (e.g., Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays), I couldn't pinpoint the exact cause of the error.

What could I have done incorrectly?

Stacklitz:

https://stackblitz.com/edit/angular-l6yvgd?file=src/app/app.module.ts

Initialization:

  import { Observable } from 'rxjs/internal/Observable';
  allIngridients: Observable<Ingridient[]>;
  selectedIngridient: Ingridient;

Data Model:

    export class Ingridient {
    _id: string;

    name: string;// todo: Übersetzungen
    barCode?: Barcode;

    constructor() {
        this._id = UUID.UUID();
    }
}

HTML Markup:

      <mat-form-field>
            <mat-select placeholder="Select an ingridient" [(value)]="selectedIngridient">
              <mat-option *ngFor="let ingridient of allIngridients" [value]="ingridient._id">
                {{ingridient.name}}
              </mat-option>
            </mat-select>
          </mat-form-field>

Populating allIngridients (called in the constructor):

  inlineFunction_IngridientsNumberAndUnits_Ingridient(recipeId: string, quantity: number): Ingridient[] {
    let result: Ingridient[] = [];

    for (let i = 0; i < quantity; i++) {

      const ingridient: Ingridient = new Ingridient(recipeId);

      // Assign values
      ingridient.name = "Unit_" + (i + 1);
      ingridient.name = "BarCodes_" + (i + 1);

      // Subscription
      this.allIngridients.subscribe(current => {
        current.push(ingridient);
      })

      result.push(ingridient);
    }

    return result;
  }

Answer №1

The line you see below is not an array, which is why you are unable to iterate over it. It is observable and meant to hold an array. To iterate over it, you must subscribe and assign the result to an array.

allItems: Observable<Item[]>;

Alternatively, you can use the | async pipe:

<mat-option *ngFor="let item of allItems | async" [value]="item._id">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The specified type does not meet the constraint as it lacks the required index signature

I'm currently working on refactoring a TypeScript project that utilizes React Hooks. While I have some knowledge of TypeScript, I am still more of a beginner than an expert. My main goal is to create reusable code for this project through the use of ...

Micro Frontend: Implementing a Pubsub System for Scalable Front-End Architecture

Currently, I am developing a large-scale application using the innovative micro front end architecture. The application is split into 5 Micro Apps: (micro-app-A (developed in Vue), micro-app-B (built with Vue), micro-app-C (using Angular), micro-app-D (cre ...

Learning to implement forwardRef in MenuItem from Material-UI

Encountering an error when pressing Select due to returning MenuItem in Array.map. Code const MenuItems: React.FC<{ items: number[] }> = (props) => { const { items } = props; return ( <> {items.map((i) => { return ( ...

Communicating Data Between Controllers in Node.js

Within my PlantsController, I extract the Plants using the following method: function getAll() { const plants= HttpRequestPromise.get(config.ApiURL+'allPlants', config.head); return plants; } export class PlantsController { ... public ...

How can I place an icon on a specific location in a Highcharts map?

I am currently utilizing Highcharts to showcase maps within my application. I am aiming to achieve two specific functionalities: 1) Upon clicking on any area, I want that area to be highlighted in red {completed} 2) Upon clicking on any area, I intend f ...

Exploring the latest upgrades in React 18 with a focus on TypeScript integration

I am currently working on a complex TypeScript project with React and recently made the decision to upgrade to the new version of React 18. After running the following commands: npm install react@18 npm install react-dom@18 npm install @types/react-dom@18 ...

The underscore convention for defining members in Typescript allows for clear and concise

Let's talk about a class called Email class Email { private _from: string; private _to: Array<string>; private _subject: string; } When an email object is created, it will look something like this: { _from:'', _to:'&apo ...

Error: The module 'AppModule' has encountered an unexpected value 'CalendarComponent'. To resolve this issue, please include a @Pipe/@Directive/@Component annotation

Recently, I started working with Angular 2 and wanted to incorporate the 'schedule' feature from Primeng. To do so, I installed its package and added the calendarComponent in my app.module.ts file as shown below: import { BrowserModule } from &a ...

javascript identify dissimilarities within arrays

Working on an Angular 2 application and attempting to identify the difference between two arrays (last seven days and missing dates within the last seven days). Everything works fine when initializing the array through a string, like in example code 1. How ...

What are the TypeScript types needed for a React component that accepts an array of objects as a prop?

I am currently working on a React component that includes a prop named propWhichIsArray. This prop is expected to be an array of objects. Each object in the array will contain properties such as id (an ID) and text (a string). How do I properly define this ...

Defining a JSON file interface in Angular to populate a dropdown box with dependencies

I've embarked on an exciting project to develop a cascading dropdown box filter, and it's proving to be quite challenging. I'm taking it step by step to ensure clarity. I have obtained a JSON file containing the data required to populate de ...

Could an OpaqueToken be assigned using an observable?

I am attempting to establish an opaque token in the providers using an observable. The purpose behind this is that I am retrieving the value through the Http provider (from an external JSON file). This is my current approach: { provide: SOME_ ...

What is the best way to calculate the difference between two dates using Angular?

How can I subtract two variables of type Date in Angular? Is there a built-in method for this, or do I need to create my own? This is what I have attempted: test: Date = new Date(); var1: Date = new Date('20-08-2018'); var2: Date = new Da ...

Error Page Reappeared After Refresh

I have a backend Spring Boot Application and I am using Angular 2 Single Page Application for the frontend. When I navigate to a specific route, such as localhost:8080/getAccounts, and then refresh the page, I encounter the Whitelabel Error Page. However, ...

Learn how to set expectations on the object returned from a spied method, Jasmine

I am currently faced with setting an expectation regarding the number of times a specific function called "upsertDocument" is executed. This function is part of a DocumentClient object within a getClient method in production. Here's how it looks: cli ...

How to Override Global CSS in a Freshly Created Angular Component

My CSS skills are a bit rusty and I need some assistance with a project I'm working on. The project includes multiple global CSS files that have properties defined for different tags, such as .btn. However, these global CSS files are causing conflicts ...

"Enhancing global accessibility through i18n strategies and evolving language usage

I am looking to update the terminology used in my Angular application. Essentially, I need i18n functionality, but I am not interested in changing the language or implementing localization. The app will remain in English, and I simply want to modify certa ...

Error: Attempting to modify a constant value for property 'amount' within object '#<Object>'

After fetching data from an API, I stored an array in a state. Upon trying to update a specific field within an object inside the array using user input, I encountered the error message: 'Uncaught TypeError: Cannot assign to read only property 'a ...

Retrieve information from Angular 2 response

In my code, I am working with 1 component and 1 service. Here is the component element : ... constructor(private requestService : RequestService) { } ngOnInit() { this.a = this.requestService.send_request(urlWebAPI); console.log(this.a); } ... ...

Issue encountered while declaring a variable as a function in TSX

Being new to TS, I encountered an interesting issue. The first code snippet worked without any errors: interface Props { active: boolean error: any // unknown input: any // unknown onActivate: Function onKeyUp: Function onSelect: Function onU ...