Search code examples
angularangularfire2angularfire5angular6

Angular 6 / AngularFire migration


I have the following older Angular 4 code:

The navigation.component.ts file:

import { Component } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
import { LogoComponent } from './logo.component';

@Component({
    selector: 'app-navigation',
    templateUrl: './navigation.component.html'
})

export class NavigationComponent {
    items: FirebaseListObservable<any[]>;
    constructor(db: AngularFireDatabase) {
        this.items = db.list('/pages', { 
            query: {
                orderByChild: 'sortOrder',
                limitToLast: 100
            }
        });
    }
}

The navigation.component.html file (truncated for brevity):

<nav class="nav-standard">
    <app-logo></app-logo>
    <div class="nav-dropdown">
        <ul *ngFor="let item of items | async | filter : 'parent' : '00000000-0000-0000-0000-000000000000'" class="nav-dropdown">
            <li>

            </li>
        </ul>   
    </div>
</nav>

<nav class="nav-narrow">
    <app-logo></app-logo>
    <md-menu #menu="mdMenu" class="nav-dropdown nav-narrow-dropdown">
        <ul *ngFor="let item of items | async | filter : 'parent' : '00000000-0000-0000-0000-000000000000'" class="nav-dropdown nav-narrow-dropdown">
            <li>

            </li>
        </ul>   
    </md-menu>
    <button md-icon-button [mdMenuTriggerFor]="menu" class="expand-button">
        <md-icon>more_vert</md-icon>
    </button>
</nav>

which I am trying to convert to Angular 6 [and got this far]:

The navigation.component.ts file:

import { Component } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { LogoComponent } from './logo.component';

@Component({
    selector: 'app-navigation',
    templateUrl: './navigation.component.html'
})

export class NavigationComponent {
    items: AngularFireList<any[]>;
    constructor(db: AngularFireDatabase) {
        this.items = db.list('/pages');
    }
}

The navigation.component.html file (truncated for brevity):

<nav class="nav-standard">
    <app-logo></app-logo>
    <div class="nav-dropdown">
        <ul *ngFor="let item of items | async | filter : 'parent' : '00000000-0000-0000-0000-000000000000'" class="nav-dropdown">
            <li>

            </li>
        </ul>   
    </div>
</nav>

<nav class="nav-narrow">
    <app-logo></app-logo>
    <mat-menu #menu="matMenu" class="nav-dropdown nav-narrow-dropdown">
        <ul *ngFor="let item of items | async | filter : 'parent' : '00000000-0000-0000-0000-000000000000'" class="nav-dropdown nav-narrow-dropdown">
            <li>

            </li>
        </ul>   
    </mat-menu>
    <button mat-icon-button [matMenuTriggerFor]="menu" class="expand-button">
        <mat-icon>more_vert</mat-icon>
    </button>
</nav>

This is throwing an error:

ERROR Error: InvalidPipeArgument: '[object Object]' for pipe 'AsyncPipe' at invalidPipeArgumentError (common.js:4238) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe._selectStrategy (common.js:4845) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe._subscribe (common.js:4835) at AsyncPipe.push../node_modules/@angular/common/fesm5/common.js.AsyncPipe.transform (common.js:4817) at Object.eval [as updateDirectives] (NavigationComponent.html:4) at Object.debugUpdateDirectives [as updateDirectives] (core.js:11922) at checkAndUpdateView (core.js:11315) at callViewAction (core.js:11556) at execComponentViewsAction (core.js:11498) at checkAndUpdateView (core.js:11321)

It appears that switching to the new AngularFireList is breaking the ngFor? I am a bit confused as to why?


Solution

  • Async pipe subscribes to Observable or Promise. In your case, 'items' is only a reference to the AngularFireList object. You probably need to turn it into an Observable:

        items: Observable<any[]>;
        constructor(db: AngularFireDatabase) {
          this.items = db.list('/pages').valueChanges();
        }