Antonio Ooi
Antonio Ooi

Reputation: 1828

Firebase Database [v6.2.4]: Snapshot not Exist

I have the following Firebase database:

enter image description here

I just want to retrieve a set of Accounts records and later bind it with ng-repeat as shown below:

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

I use the Automatic version of Firebase references and initialization as shown below, therefore no apiKey and databaseURL have been specified here:

<!-- 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>

When I do a simple check on the snapshot, it is always not exist (no runtime error whatsoever):

        // 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!");
            }

        })

I tried ref("/Accounts") and ref("Accounts/") but no luck. Sorry that I'm very new to Firebase real-time database. I just want to know:

  1. How can I retrieve a record set for my Accounts database table and bind it with ng-repeat as shown above?
  2. Have I structured my data wrongly (see screenshot)? Note that OTK-001 is an unique account code for each record in the Accounts table/collection.
  3. Do I still need to add the old Firebase config with apiKey and databaseURL like the old way? (NOTE: Tried before, not working.)

Any help will be appreciated, thanks in advance!

Upvotes: 1

Views: 71

Answers (1)

Ram
Ram

Reputation: 376

Firestore querying is like the below

collection reference for firestore is

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

Fetching the data is

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

Upvotes: 1

Related Questions