The ngFor loop encounters an undefined value and halts its execution

How can I make *ngFor continue even when it hits an undefined value instead of stopping? When I remove {{order.shipping.name}}, the other two interpolations work fine.

This is the component:

import { Observable } from 'rxjs/Observable';
import { Component } from '@angular/core';
import { OrderService } from '../../order.service';
import { FirebaseListObservable } from 'angularfire2/database-deprecated';

@Component({
    selector: 'app-admin-orders',
    templateUrl: './admin-orders.component.html',
    styleUrls: ['./admin-orders.component.css']
})
export class AdminOrdersComponent {
    orders$;

    constructor(private orderService: OrderService) {
        this.orders$ = orderService.getOrders();
    }

}

And here's the template:

<table class="table table-striped table-bordered table-hover">
    <thead>
        <tr>
            <th>date</th>

            <th class="text-center">id</th>
            <th class="text-right">id</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <div *ngFor="let order of this.orders$ | async: date">
                    {{order.shipping.name}}

                </div>
            </td>
            <td class="text-center">
                <div *ngFor="let order of this.orders$ | async">

                    {{order.datePlaced | date:"short"}}
                    <!-- https://angular.io/api/common/DatePipe -->
                </div>
            </td>
            <td class="text-right">
                <div *ngFor="let order of this.orders$ | async">
                    <a> {{order.userId}} </a>

                </div>
            </td>
        </tr>
    </tbody>
</table>

I found a solution here and made this change:

orders$: FirebaseListObservable<any>; in the component

{{order?.shipping.name}} in the template

However, it didn't solve the issue for me.

Answer №1

Adding another safe navigation operator is necessary after the shipping section too,

 {{order?.shipping?.name}}

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

When the data is not initialized within the ngOnInit() function, the p-dataTable does not bind properly

In the process of building a form, there is a specific dialog available for users to input data. The dialog functions within a larger form structure. Once the user finishes entering their data in the dialog and clicks on the SAVE button, the entered inform ...

What is the proper way to manage the (ion select) OK Button?

Hey there, I'm working with an Ionic select directive and I need to customize the functionality of the 'OK' button. When I click on it, I want it to call a specific function. I'm aware of the (ionChange) event, but that only triggers w ...

Organizing a store in a hierarchical structure

I am currently facing an issue with creating a hierarchical store in Angular. My goal is to have multiple reducers that work on specific parts of the application. Here's the scenario: I am working on an HR application that has a talent (employee) mod ...

Using the ternary operator in React to implement inline styles

Within my React/Typescript project, I aim to dynamically exhibit a color based on the presence or absence of a value in payload[1]. In the code snippet below, note the usage of an inline style tag. <li className="recharts-tooltip-item" style={ ...

Customizing key values within nested objects in Angular2: A comprehensive guide

I need assistance with setting up a JSON object for a post in Angular2/Typescript. I am trying to dynamically set the nested object values for a key. Is it possible to achieve this using Angular2/Typescript? I have attempted to retrieve the values from JS ...

Close the menu in Angular 7 by clicking outside of it

I am seeking a way to detect when a click event occurs inside the parent div #menu, regardless of any HTML tags present within it. Using nativeElement.parent did not provide the desired result. Here is the HTML code: <button #toggleButton (click)="to ...

Restricting a Blob to a particular data type

As seen in the GitHub link, the definition of Blob is specified: /** A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functional ...

The Ionic serve command fails to recognize and reflect any saved changes, leading to a lack of automatic reloading

Recently, I encountered a strange issue while using my ionic 2 application. Whenever I execute the ionic serve command, it launches the server on localhost and produces the following output: [12:00:45] ionic-app-scripts 0.0.45 [12:00:46] watch started ...

Is it possible to globally define a namespace in Typescript?

Seeking a way to make my Input module accessible globally without the need for explicit path definitions. Currently, I have to import it like this: import { Input } from "./Input/Input";. Is there a method to simplify the import statement for modules con ...

What are the methods to determine the cause of ESLint's slow performance?

Looking to analyze the performance of ESLint in my application. So far, I have only come across one profiling tool provided by ESLint which is the TIMING=1 environment variable. Combining this with DEBUG=eslint:cli-engine allows me to see timing results pe ...

Can Angular 4 experience race conditions?

Here is a snippet of my Angular 4 Service code: @Injectable() export class MyService { private myArray: string[] = []; constructor() { } private calculate(result): void { myArray.length = 0; // Perform calculations and add results to myAr ...

Form control named 'startTime' and 'endTime' do not have a valid value accessor

The app.module file contains the following code: import { TimepickerModule, TimepickerConfig } from 'ngx-bootstrap/timepicker'; import { getTimepickerConfig } from './TimepickerConfig'; imports: [ BsDatepickerModule.forRoot(), Tim ...

Tips for troubleshooting JavaScript in an Angular 5 application using Visual Studio 2017

I recently developed an Angular 5 web application using VS2017. Initially, the app was functioning well until I decided to enable javascript debugging. Post that change, upon launching the app, I encountered the following error: https://i.sstatic.net/ygq ...

What is the best way to obtain a reference to an instance of my Angular 2 directive?

Angular 2 rc 5 was written using typescript 1.9 I am trying to access the instance of my attribute directive. Although I am using ViewChild, which typically works with components, it is giving me a handle to the element containing the directive. template ...

What is the reason behind typescript-eslint's insistence on using camelCase for enumMember?

The TypeScript documentation showcases enum examples with PascalCase for enum members, like: this enum Direction { Up = 1, Down, Left, Right, } However, @typescript-eslint/naming-convention mandates camelCase over PascalCase, resulting in: enum Di ...

Guide to Updating a Global Variable in Angular 2

I have been working with Angular 2 and I came across an issue with global variable access. To solve this, I created a service and injected it into my component pages. While I can access the variable in any page, updating it does not reflect the changes wh ...

Expand by focusing solely on recognized attributes?

I am working on creating an interface that can accept a mapped type, allowing for both runtime logic and compile-time typing to be utilized. Here is an example of what I'm aiming for: type SomeType = { a: string b: { a: string, b: string } } magi ...

Having trouble migrating Angular with Nativescript? Encounter an issue parsing the angular.json file

Attempting to enhance Angular with Nativescript by following the provided instructions has led to a puzzling issue. Whenever the command to add nativescript-schematics (ng add @nativescript/schematics) is executed, an error message stating "File angular.js ...

Receiving the error notification from a 400 Bad Request

I'm having trouble showing the errors returned from an API on my Sign up form when a user submits invalid data. You can check out the error response in the console here: console ss This is my approach in RegisterComponent.ts: onSubmit() { this.u ...

Having issues with typings after npm post-install execution

My current packages.json file looks like this: { "name": "shopping-assistant-angular", "version": "1.0.0", "scripts": { "start": "concurrent \"npm run tsc:w\" \"npm run lite\" ", "tsc": "tsc", "tsc:w": "tsc -w", "li ...