2017-07-12 14 views
0

我試圖調用一個函數,以便知道一家公司是否已經達到了允許的最大註冊數量,但每次我稱之爲它都不會打印任何內容。 有誰知道我在做什麼錯?試圖從一個函數得到真或假不會做任何事

謝謝!

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('01').subscribe(result => { 
 
      console.log(result) //true or false 
 
    })

+0

你調試了你的代碼嗎?你的代碼是否到達'return countRegs> = maxRegs;'行? – echonax

+0

您是否看到其他'console.log'的結果? –

+0

從文檔中,我明白當使用'forkjoin'時,如果其中一個observables沒有發出任何值,你的'subscription'將不會被調用。 在這裏檢查https://www.learnrxjs.io/operators/combination/forkjoin.html和這裏http://reactivex.io/documentation/operators/zip.html –

回答

0

嘗試:

this.maxRegsReached('01') 
    .map(response => response) 
    .subscribe(result => { 
     console.log(result) //true or false 
    }) 
+0

同樣的事情,它進入功能,但沒有輸出:/ – Ace

+0

'maxRegs'和'getCountRegs'控制檯日誌激發嗎?他們的輸出是什麼?如果您將maxRegsReached控制檯更改爲「console.log(」Result:「,result)// true或false」,您是否在控制檯上看到「Result:」? –