Angular 5 is throwing an error stating that it cannot read the property 'text' of undefined

I have developed a question component where I have added some predefined questions. However, when I attempt to execute the application, it displays 'undefined' text.

Below is my component code:

import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-question-list',
templateUrl: './question-list.component.html',
styleUrls: ['./question-list.component.css']
})
export class QuestionListComponent implements OnInit {
questions: Object[];
constructor() {
this.questions = [
  {
    text: 'What is your name?',
    answer: 'My name is Saeef'
  },
  {
    text: 'What is your favorite color?',
    answer: 'My favorite color is blue'

 ];
 }

 ngOnInit() {
 }

 }

This is the HTML code for the component:

<div class="question" *ngFor let question of questions">
<div class="card">
    <div class="card-header">
        {{ question.text }}
    </div>
    <div class="card-block">
        <p class="card-text">{{ question.answer }}</p>
    </div>
</div>
</div>

Answer №1

Your ngFor syntax needs correction,

The correct syntax should be:

<div class="question" *ngFor="let question of questions">
<div class="card">
    <div class="card-header">
        {{ question.text }}
    </div>
    <div class="card-block">
        <p class="card-text">{{ question.answer }}</p>
    </div>
</div>
</div>

Answer №2

Implement the *ngFor directive with the following syntax: {{ quiz['questionText'] }} {{ quiz['answerText'] }}

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

Updating the navigation bar in Node/Angular 2 and displaying the sidebar once the user has logged in

I am facing a challenge with my first project/application built using Angular 2, particularly related to the login functionality. Here is what I expect from the application: Expectations: When I load the login component for the first time, the navbar ...

retrieve the initial subarray from the array using rxjs

Looking to extract the first array from a subarray, my current setup is as follows: Map: map; Map() { Service }); } This is the interface structure: export interface map { } Encountering an error message: ERROR TypeError: ...

What is the best approach to implement server-side rendering in Next.js while utilizing Apollo React hooks for fetching data from the backend?

I have a Next.js project that is utilizing Apollo GraphQL to retrieve data from the backend. My goal is to render the page using server-side rendering. However, I am encountering an obstacle as the React hooks provided by GraphQL Apollo prevent me from cal ...

Issue with Angular Datatable not displaying content when [dtTrigger] directive is included in the HTML table - Angular 14

Upon adding [dtTrigger] = "dtTrigger" to the HTML table, I encountered an issue where the datatable ceased rendering. All the features of the datatable were no longer visible on the page. Removing [dtTrigger] = "dtTrigger" from the HTML ...

Looking for a TypeScript annotation that allows accessing an object property using a variable

When working with plain JavaScript, we have the ability to access an object's property value using a variable. For example, this is permitted: let obj = { a: 100, b: 'Need help with TypeScript', c: new Date() }; let prop = 'b'; c ...

Easily Organize Your Data with Kendo React Grid: Rearrange Columns and Preserve Your Custom Layout

Currently, I am working on implementing a Kendo React grid with the option set to reorderable={true} for allowing column reordering. However, I am facing difficulty in determining how to save the new order of columns. Which event should I use to detect whe ...

What is the local date format for the Ionic DatePicker?

I have successfully implemented a DatePicker in my Ionic Project, but the date is displaying in the wrong time format. Here is my function: showDatePicker(){ this.datePicker.show({ date: new Date(), mode: 'date', allowOldDates: fal ...

When should services be included within providers?

After completing a small Angular 2 project, I compared my work with the provided code and realized that in the app.module.ts file, only one of the two services I created was included by the tutor. users.service.ts import { Component, Injectable } fro ...

Is there a way to get this reducer function to work within a TypeScript class?

For the first advent of code challenge this year, I decided to experiment with reducers. The following code worked perfectly: export default class CalorieCounter { public static calculateMaxInventoryValue(elfInventories: number[][]): number { const s ...

Elevate the Appearance of Material UI Elements with custom CSS Sty

I am currently facing an issue while trying to customize the styling of a Material UI component using CSS. Here is the code snippet: <IconButton className="my-class"> <Close /> </IconButton> CSS: .my-class { float: right ...

The IDE is showing an error, but Jest is able to run it flawlessly

I recently created a Jest unit test for a TypeScript function called checkEmail, which internally uses showAlert. The showAlert function in the utils.ts file looks like this: export const showAlert = (message: string) => { toast(message); }; In my ...

The .env file setting does not take precedence over the default value in the

I am facing an issue with my JSON file that contains the default convict configuration. Here is a simplified version of the file: { "env": { "doc": "The application environment.", "format": [" ...

Tips for resolving the error when setting up @ngrx/store?

I'm encountering an issue during the installation of @ngrx/store. Is there anyone who can assist me in resolving this error? PS C:\Users\Welcome\ngrx-test> npm install @ngrx/store npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to re ...

Having trouble mocking useAppSelector in Jest, RTL, Redux Toolkit, and React testing

I have react redux toolkit installed and have replaced vitest with jest for testing purposes. My goal is to test whether the modal window is rendered in the App component when the isOpen flag is true. I only mock the part of the store that is necessary, n ...

Issues encountered while accessing REST in Angular 6 due to Access-Control-Allow-Origin restrictions

I am currently facing an issue with loading data from a REST source into my Angular 6 app using http: HttpClient from '@angular/common/http'. When I try to call the app in the browser using ng serve --open, it seems like CORS is causing a problem ...

What is the most efficient way to use map-reduce in TypeScript to filter a list based on the maximum value of an attribute?

Recently, I came across a list that looked something like this: let scores = [{name: "A", skills: 50, result: 80}, {name: "B", skills: 40, result: 90}, {name: "C", skills: 60, result: 60}, {name: "D", skills: 60, ...

Unlocking the accordion feature in Ionic 3 form: A step-by-step guide

I am currently working on a search form and want to incorporate it within an accordion so that users can simply click to expand the form. Below is the code snippet: TS. buildForm(): void { this.form = this.fb.group({ username: new FormControl(& ...

What is the best way to retrieve app.state in a Remix project when running a Cypress test?

One way Cypress can expose an app's state to the test runner is by using the following approach in React: class MyComponent extends React.Component { constructor (props) { super(props) // only expose the app during E2E tests if (window.C ...

Updating Angular 9 Reactive Form: How to Use PatchValue with a Nested FormArray in a FormGroup

I am currently working on maintaining an existing project that involves using a FormGroup helper to transform data into the FormGroup format. The FormGroup includes four nested FormArray elements, and my task is to update all the data within the FormGroup ...

I desire for it to effortlessly unlock within my matmenu as soon as the webpage loads

Upon opening my page, I want the material menu to automatically open. However, since there is no click action, I am encountering an error stating that "trigger" is undefined. An error occurred: TypeError: Cannot read properties of undefined (reading &apo ...