angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Syntax not working as documented.

Open eat-sleep-code opened this issue 8 years ago • 3 comments

I am using Angular ^6.0.3, rxjs ^6.2.0, and angularfire2 ^5.0.0-rc.10

Followed the documentation about getting mapping in the key as outlined in version-5-upgrade.md:

However having an issue where return items.map(item => items.key); code indicates that:

[ts] Property 'map' does not exist on type '{}'.

and at build time:

ERROR in src/app/navigation.component.ts(40,1): error TS1128: Declaration or statement expected.

Here is the full code to the component. Is there a syntax error, or is the documentation not clear as to what imports are required?

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

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


export class NavigationComponent {
    items: Observable<any[]>;
    childItems: Observable<any[]>;

    constructor(db: AngularFireDatabase) {
        this.items = this.getNavigation(db);
        this.childItems = this.getNavigation(db);
    }

    getNavigation(db): any {
        let pageList = db.list('/pages', ref => {
            let query = ref.limitToLast(100).orderByChild('sortOrder');
            return query;
        })
        
        return pageList.snapshotChanges().pipe(
            map(pages => 
                pages.map(p => ({ key: p.key, ...p.payload.val() }))
            )
        ).subscribe(items => {
            return items.map(item => item.key);
        });
    }
}

eat-sleep-code avatar Jun 06 '18 17:06 eat-sleep-code

A) I don't understand the point of your return in subscribe... are you meaning to map that or assign it to a variable instead? Also returning the .subscribe isn't going to do what you expect, it will return the subscription not the observable or value. If you're intent is logging you should use the tap operator.

B) your ultimate error is because you have an extra closing curly brace at the end }

jamesdaniels avatar Jun 06 '18 22:06 jamesdaniels

For anyone who runs into a similar issue...

In addition to removing the errant curly brace that @jamesdaniels pointed out, I had to:

  • type the db parameter of the getNavigation function.
  • remove the subscribe() that was shown in the documentation, but was not necessary in my use case.

Here is my final working code:

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

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


export class NavigationComponent {
    items: Observable<any[]>;
    childItems: Observable<any[]>;

    constructor(db: AngularFireDatabase) {
        this.items = this.getNavigation(db);
        this.childItems = this.getNavigation(db);
    }

    getNavigation(db: AngularFireDatabase): any {
        return db.list('/pages', ref => {
            let query = ref.limitToLast(100).orderByChild('sortOrder');
            return query;
        }).snapshotChanges().pipe(
            map(pages => {
                return pages.map(p => ({ key: p.key, ...p.payload.val() }));
            })
        );
    }
}

eat-sleep-code avatar Jun 07 '18 04:06 eat-sleep-code

in the spirit of passing it on like @eat-sleep-code, you need to use a return statement in the map function inside the pipe to avoid any undefined errors in your subscribe.

Thanks @eat-sleep-code. You saved my day with this example.

Here's my working code example (gives your the key and value):

private db : AngularFireDatabase

  this.db.list('team/'+this.ifiAuth.userMeta.teamRef).snapshotChanges().map(actions => {
    return actions.map(a => ({key: a.key, value: a.payload.val()}));
  }).subscribe(
    (items) => {
      console.log('Data with key:', items);
    }
  )

audacitus avatar Jun 09 '18 08:06 audacitus