【问题标题】:Load data from Firebase Firestore to array - angularjs将数据从 Firebase Firestore 加载到数组 - angularjs
【发布时间】:2019-04-25 14:44:06
【问题描述】:

我正在尝试从 Firestore 获取数据。连接正常,正在下载数据但有问题。

我基于https://firebase.google.com/docs/firestore/query-data/get-data 示例。

let list = [];

let products = function GetCollection() {
    firestore.collection("products")
        .get()
        .then(function (querySnapshot) {
            querySnapshot.forEach(function (doc) {
                console.log(doc.id, " => ", doc.data());
                list.push(doc.data());
            });
            $scope.collection = list;
        });
}
products();

console.log("test");

运行此代码后,我的列表为空,但在控制台中列出了所有条目。在调试过程中,我注意到最后一行 console.log("test") 在 GetCollection() 函数的所有主体之前执行。 这是同步的问题?谁能帮我?

【问题讨论】:

    标签: angularjs firebase google-cloud-firestore


    【解决方案1】:

    是的,这是预期的行为,console.log("test"); 将首先执行。因为.then(function (querySnapshot 部分是异步调用,所以你在.then 中传递的是一个javascript 承诺,这是异步的

    您还应该使用下一行中提到的句柄错误 .get(some comde). then ( function -- ). catch ( function (error) { console.log("Error getting cached document:", error); } )

    出于测试目的,你也可以使用firebase同步版本的方法,一般firebase api都有相应的同步版本方法,如果我没记错的话都是以“sync”为后缀的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      相关资源
      最近更新 更多