2016-09-28 41 views
3

所以我有下面的代码获取FirebaseObjectObservable。但路径是动态的,因为它可能还没有。所以如果路径不在那里,我想创建这条路径。但如果它在那里,我想更新/修补数据。我如何知道FirebaseObjectObservable为空?

this.userLocationDetail = this.af.database.object('/userProfile/' + uid + '/bbwList/' + afLocationId); 
    if (this.userLocationDetail) { 

    console.log('Data is found'); 

    } else { 
    console.log('Data not found'); 
    } 

问题是如果(this.userLocationDetail)将始终为真。我怎样才能看到可观察的并确定它是空的?

回答

3

你可以在可观察的管道内找到。如果你想返回一个Observable<boolean>,你可以.map。或者你可以在管道内用它做点什么。

this.af.database.object('/userProfile/' + uid + '/bbwList/' + afLocationId) 
    .subscribe(x => {  
     if (x.hasOwnProperty('$value') && !x['$value']) { 
      console.log('data is not found'); 
     } else { 
      console.log('data is found'); 
     } 
    }); 

如果你只是想一个Observable<boolean>

this.af.database.object('/userProfile/' + uid + '/bbwList/' + afLocationId) 
    .map(x => {  
     if (x.hasOwnProperty('$value') && !x['$value']) { 
      return false; 
     } else { 
      return true; 
     } 
    }); 

还是更浓缩版本:

this.af.database.object('/userProfile/' + uid + '/bbwList/' + afLocationId) 
    .map(x => !x.hasOwnProperty('$value') || x['$value']); 
+0

谢谢!任何示例以及可观察的方法? –

+0

我添加了一个可观察的代码