如果key为true,则返回false

时间:2017-09-12 17:57:41

标签: javascript angular firebase-realtime-database

我想仅在当前用户是我使用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列表eventsGuestsLookup如下所示:

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

this.uiduid2eventKey-Ktm57w59Q_2c48r6ntx一个事件密钥,甚至不在uid2列表中。我希望this.hello返回false,但是数据库中唯一的事件是为用户uid2显示的......我做错了什么?

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;
  }

2 个答案:

答案 0 :(得分:0)

你在对象上做&#39; map&#39; 吗?

如果要映射结果,应使用 db.list

Angular firebase

答案 1 :(得分:0)

您正在使用Boolean对象而不是boolean原语。这可能是问题吗?

编辑/更新(2017年9月13日):

我根本没有使用过angularfire,但是从您的代码看起来,您的ngIf谓词函数checkGuest()正在返回FirebaseObjectObservable<boolean>我认为可能是truthy - 始终不管其boolean值(因为它被包装为可观察的)。

我认为如果您将checkGuest()方法改为此

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

它可能会奏效。如果你还在寻找答案,试试吧。

相关问题