evsc
evsc

Reputation: 97

Firebase Database reference snapshot is not returning any data

I am trying to read some dummy data I have put into my Firebase database but cannot get it to return anything despite trying my best with following the documentation.

I have a JS file with Firebase initialized, and with this variable exported:

const database = firebase.database();

In a file I'm trying to read it in, I have the below:

import database from './firebase';
const dbRef = database.ref('users/');
dbRef.on('value', (snapshot) => {
  console.log(snapshot.val());
});

Nothing gets logged out into the console. But when I log out just dbRef, I get the Reference object so it does seem to be connected properly:

Reference {repo: Repo, path: Path, queryParams_: QueryParams, orderByCalled_: false}
database: (...)
key: (...)
orderByCalled_: false
parent: (...)
path: Path {pieces_: Array(1), pieceNum_: 0}
queryParams_: QueryParams {limitSet_: false, startSet_: false, startNameSet_: false, endSet_: false, endNameSet_: false, …}
ref: (...)
repo: Repo {repoInfo_: RepoInfo, app: FirebaseAppImpl, dataUpdateCount: 0, statsListener_: null, eventQueue_: EventQueue, …}
root: (...)
__proto__: Query

I can't find where I've went wrong, would really appreciate the help. In my Firebase console, I am using Cloud Firestore by default rather than Realtime Database, not sure if that's making a difference.

Upvotes: 1

Views: 1164

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

You're trying to use the Realtime Database API instead of the Firestore API. They are not at all the same database, and they don't share the same API. Use the Firestore docs to get started. You will use firebase.firestore() instead of firebase.database(). The API docs for Firestore start here.

Upvotes: 2

Related Questions