简体   繁体   English

如果key为true,则返回false的true

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

{
  "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. this.uiduid2eventKey-Ktm57w59Q_2c48r6ntx事件的关键是不是即使在uid2列表。 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? 我希望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;
  }

Are you doing 'map' on an object? 您是否正在对对象进行“映射”

If you want to map through the results you should use db.list . 如果要映射结果,则应使用db.list

Angular firebase 角火力基地

You are using a Boolean object instead of a boolean primitive. 您正在使用Boolean对象而不是boolean元。 Could that be the issue? 这可能是问题吗?

EDIT/UPDATE (Sept 13, 2017): 编辑/更新(2017年9月13日):

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). 我根本没有使用过angularfire ,但是从您的代码来看,它看起来像您的ngIf谓词函数checkGuest() ,正在返回FirebaseObjectObservable<boolean> ,无论它的boolean值如何,它truthytruthy (因为它被包装了)作为可观察到的)。

I think if you change your checkGuest() method to this, 我想如果您将checkGuest()方法更改为此,

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. 如果您仍在寻找答案,请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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