Firebase数据库[v6.2.4]:快照不存在

时间:2019-08-27 10:19:34

标签: firebase google-cloud-firestore

我有以下Firebase数据库:

enter image description here

我只想检索一组Accounts记录,然后将其与ng-repeat绑定,如下所示:

<table>
  <tr ng-repeat="account in accounts">
    <td>{{ account.AccName }}</td>
    <td>{{ account.AccStatus }}</td>
  </tr>
</table>

我使用的是自动版本的Firebase引用和初始化,如下所示,因此此处未指定apiKeydatabaseURL

<!-- update the version number as needed -->
  <script defer src="/__/firebase/6.2.4/firebase-app.js"></script>
  <!-- include only the Firebase features as you need -->
  <script defer src="/__/firebase/6.2.4/firebase-auth.js"></script>
  <script defer src="/__/firebase/6.2.4/firebase-database.js"></script>
  <script defer src="/__/firebase/6.2.4/firebase-messaging.js"></script>
  <script defer src="/__/firebase/6.2.4/firebase-storage.js"></script>
  <!-- initialize the SDK after all desired features are loaded -->
  <script defer src="/__/firebase/init.js"></script>

当我对snapshot进行简单检查时,它总是不存在(无论如何都没有运行时错误):

        // ref("Accounts") is my root ref as shown in picture above
        const accountsRef = firebase.database().ref("Accounts"); 

        accountsRef.once("value").then(function (snapshot) {
            if (snapshot.exists()) {
                console.log(snapshot.val());
            } else {
                console.log("Snapshot not exist!");
            }

        })

我尝试了ref("/Accounts")ref("Accounts/"),但是没有运气。抱歉,我是Firebase实时数据库的新手。我只想知道:

  1. 如何为我的Accounts数据库表检索记录集,并将其与ng-repeat绑定,如上所示?
  2. 我是否对数据进行了错误的构造(请参见屏幕截图)?请注意,OTK-001Accounts表/集合中每个记录的唯一帐户代码
  3. 我是否仍需要像旧方法一样使用apiKeydatabaseURL添加旧的Firebase配置? (注意:之前尝试过,无法正常工作。)

任何帮助将不胜感激,在此先感谢您!

1 个答案:

答案 0 :(得分:1)

Firestore查询如下所示

firestore的收集参考为

let collectionRef = firestore.collection('col/doc/subcollection');

获取数据是

 async getAccountData() {
        const snapshot = await firebase.firestore().collection('accounts').get()
        return snapshot.docs.map(doc => doc.data());
    }