所以我有下面的代码获取 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) 将始终为真。如何查看 observable 并确保它是空的?

最佳答案

您可以在 observable 管道中找到。如果你只想返回一个 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']);

关于angular - 我怎么知道 FirebaseObjectObservable 是空的?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39755555/

10-11 23:23