我怎么知道 FirebaseObjectObservable 是空的?

How do I know a FirebaseObjectObservable is empty?

所以我在下面的代码中获取了 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');
  }

问题是 if (this.userLocationDetail) 永远为真。我怎样才能查看可观察对象并确保它是空的?

您可以在可观察的管道中找到。如果你想 return 只是一个 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']);