简体   繁体   中英

Return true of false if key is true

I want to display events only if the current user is a guest which I check using checkGuest(event.$key)

<md-card class="home-card" *ngFor="let event of events | async">
    <a *ngIf="checkGuest(event.$key)" [routerLink]="['event', event.$key]">
        <img class="event-home-img" src="https://lorempixel.com/300/300" />
        <p class="home-page-title">{{ event.name }}</p>
        <p class="home-page-venue">{{venueName}}</p>
    </a>
</md-card>

Firebase list eventsGuestsLookup looks like this:

{
  "uid1" : {
    "-Ktm57w59Q_2c48r6ntx" : true,
  },
  "uid2" : {
    "-KtmbLqT0U005Ndelw3S" : true,
    "-KtmcLr44X0ho1NZP_h_" : true,
    "-KtmdP2BKzhwezxa-Lbl" : true,
    "-KtoGScCD7Zq4N3KkQQG" : true
  }
}

this.uid is uid2 and eventKey is -Ktm57w59Q_2c48r6ntx an event key which isn't even in the uid2 list. I expect it to this.hello to return false but the only event in the database is being displayed for a user uid2 ... What am I doing wrong?

checkGuest(eventKey: string): Observable<Boolean> {

    const event = this.db.object(`eventsGuestsLookup/${this.uid}/${eventKey}`);

    return event.map(e => this.hello(e));
  }

  hello(e): Boolean {
    if (e) {
      return true;
    }
    return false;
  }

Are you doing 'map' on an object?

If you want to map through the results you should use db.list .

Angular firebase

You are using a Boolean object instead of a boolean primitive. Could that be the issue?

EDIT/UPDATE (Sept 13, 2017):

I haven't worked with angularfire at all, but from your code it looks like your ngIf predicate function checkGuest() , is returning a FirebaseObjectObservable<boolean> which I think might be truthy -always regardless of its boolean value (as it is wrapped as an observable).

I think if you change your checkGuest() method to this,

checkGuest(eventKey: string): boolean {
 return this.db.object(`eventsGuestsLookup/${this.uid}/${eventKey}`)
  .subscribe((e) => this.hello(e.$value));
}

it might work. Give it try if you are still looking for an answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM