【问题标题】:Function calling firebase object always returns true调用firebase对象的函数总是返回true
【发布时间】:2017-12-15 06:44:14
【问题描述】:

我正在尝试比较 2 个 firebase 对象 countregs 和 maxregs。我想返回一个像这样的布尔值,但无论值如何,我总是得到真实的。 如果打印显示 countregs = 3 和 maxregs = 5 的值,那么它应该返回 false。

谢谢!

getMaxRegs(company: string) {
    var snapshotMaxRegs = this.db.object(`companies/${company}/maxregs`,{ preserveSnapshot: true})

    snapshotMaxRegs.subscribe(snapshot => {
        console.log('maxRegs = '+ snapshot.val())
        return snapshot.val();
    });
}

getCountRegs(company: string){
    var snapshotCountRegs = this.db.object(`companies/${company}/countregs`,{ preserveSnapshot: true})

    snapshotCountRegs.subscribe(snapshot => {
        console.log('currRegs = '+ snapshot.val())
        return snapshot.val();
    });
}

maxRegsReached(company: string){
    if (this.getCountRegs(company) >= this.getMaxRegs(company)
        || this.getCountRegs(company) == this.getMaxRegs(company))
    {
        return true;
    } else {
        return false;
    }
}

【问题讨论】:

    标签: angular typescript firebase firebase-realtime-database angularfire2


    【解决方案1】:

    因为您的getMaxRegsgetCountRegs 在技术上没有返回任何内容,您将无法进行有意义的比较。

    您应该做的是在getMaxRegsgetCountRegs 方法中使用.map() 返回一个Observable,而不是订阅它。然后,您可以在maxRegsReached() 中加入两个可观察对象(使用.forkJoin)并比较返回的两个值。

    getMaxRegs(company: string) {
        var snapshotMaxRegs = this.db.object(`companies/${company}/maxregs`,{ preserveSnapshot: true})
    
        return snapshotMaxRegs.map(snapshot => {
            console.log('maxRegs = '+ snapshot.val())
            return snapshot.val();
        });
    }
    
    getCountRegs(company: string){
        var snapshotCountRegs = this.db.object(`companies/${company}/countregs`,{ preserveSnapshot: true})
    
        return snapshotCountRegs.map(snapshot => {
            console.log('currRegs = '+ snapshot.val())
            return snapshot.val();
        });
    }
    
    maxRegsReached(company: string){
    
        return Observable.forkJoin([
            this.getCountRegs(company),
            this.getMaxRegs(company)
        ]).map((joined)=>{
            let countRegs = joined[0];
            let maxRegs = joined[1];
    
            return countRegs >= maxRegs;
        })
    }
    

    现在您需要订阅maxRegsReached 才能获得结果:

    this.maxRegsReached('company name').subscribe(result => {
            console.log(result) //true or false
    })
    

    【讨论】:

    • 请注意,您仍然没有从maxRegsReached 返回任何内容。
    • @Duncan 你是对的。我自动假设他正在使用maxRegsReached 作为按钮调用或其他东西。我的错。
    • 您甚至可以这样做:.map([countRegs, maxRegs] => countRegs >= maxRegs) 并避免joined 的拼写错误。
    • @Duncan 好主意,如果 OP 无法遵循代码,只想更明确
    • 谢谢!但我试图打印结果但什么也没有:(codethis.authData.maxRegsReached(this.signupForm.value.company).subscribe(result => { console.log('result = ' + result) //true 或假})code
    【解决方案2】:

    您的函数getCountRegsgetMaxRegs 都返回undefined,它们包含的唯一return 语句嵌套在回调函数中,并且它们调用的函数是异步的。你需要开始异步思考:

    async getMaxRegs(company: string): Promise<number> {
      const snapshotMaxRegs = this.db.object(`companies/${company}/maxregs`,{
        preserveSnapshot: true});
    
      return snapshotMaxRegs.map(snapshot => snapshot.val()).toPromise();
    }
    
    getCountRegs(company: string): Promise<number> {
      const snapshotCountRegs = this.db.object(`companies/${company}/countregs`,{
        preserveSnapshot: true});
    
      return snapshotCountRegs.map(snapshot => snapshot.val()).toPromise();  
    }
    
    async maxRegsReached(company: string) : Promise<boolean>{
      const count = await this.getCountRegs(company);
      const max = await this.getMaxRegs(company);
      return count >= max; 
    }
    

    然后要调用this.maxRegsReached(),调用函数也必须声明为async,并且结果必须是await

    请始终牢记,await 表示您的所有其他代码都可以在等待结果时运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多