2017-09-16 62 views
0

我想記錄'Checker'的值(布爾值),但總是返回'undefined'。我的代碼如下:在控制檯上AngularFire2,snapshot.val返回undefined

loginUser(key: string, Checker: boolean) { 
    if (!this.loginForm.valid) { 
    *doesn't matter* 
    } else { 
    *doesn't matter* 
    ... 
    var checked = this.af.object('/Users/' + key, 
    {preserveSnapshot: true}); 
    checked.subscribe(snapshot => { 
     console.log(snapshot.key); 
     console.log(snapshot.val.Checker); 
     }); 
    } 

輸出是:

*key* 
*undefined* 
+0

'val'是一個函數:'snapshot.val()。Checker' – cartant

+0

https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#val – cartant

+0

@cartant - 如果我確實那它返回「snapshot.val不是一個函數」 –

回答

0
this.af.object(`/users/${key}`, {preserveSnapshot: true}) 
    .subscribe(snapshot => { 
    let value = snapshot.val() 
    let key = snapshot.key 

注意使用template literals,您可以使用.object內反引號(模板文本)()調用直接插入變量。您應該通讀angularfire2文檔。