【问题标题】:Carry out action only after firestore collection is returned仅在返回 firestore 集合后执行操作
【发布时间】:2020-05-27 17:09:14
【问题描述】:

我正在订阅 Firestore 中的一个集合。一旦返回值,我想对该数据执行一些操作。做这个的最好方式是什么?我遇到了一个类似的问题,导致我编写了下面的代码。但是,该代码似乎不会等到值出现。

this.versionCollection = this.FirebaseService.getCollection('versions', 'projectId', this.projectData.uid )
.subscribe(()=>{

    // Actions to be carried out after the data is returned
    this.versionData = this.versionCollection[this.versionCollection.length];
    this.initializeMenu();
    this.initializeModel();

});

afs 为 AngularFirestore 的服务中调用的代码...

getCollection( collection, paramName, paramValue ) {
    return this.afs.collection( collection, ref => ref.where( paramName, '==', paramValue )).valueChanges();
}

【问题讨论】:

    标签: angular google-cloud-firestore angularfire2


    【解决方案1】:

    像下面这样简单的 if 检查就可以完成这项工作。

    this.versionCollection = this.FirebaseService.getCollection('versions', 'projectId', this.projectData.uid )
        .subscribe(()=>{
    
            if(this.versionCollection) {
              this.versionData = this.versionCollection[this.versionCollection.length];
              this.initializeMenu();
              this.initializeModel();
            }
    
        });
    

    【讨论】:

    • 这似乎奏效了。但是,我仍然在从 observable 获取数据时遇到问题。最后,我使用了一种不同的技术——它不那么优雅,但更容易理解。我觉得我在这里错过了应该如何使用 firestore 的东西。
    猜你喜欢
    • 2013-12-23
    • 2017-05-22
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2020-07-11
    • 2019-11-24
    • 2019-06-11
    • 2021-01-23
    相关资源
    最近更新 更多