ksy
ksy

Reputation: 302

Firebase DB snapshot.val() returns null

I am using the firebase-admin (5.13.1) NodeJS library, and am having trouble getting .val() to work on a .on('value') function.

  var key = "someFirebaseKey";
  db.ref().child('search/response').child(key).on('value', function(snapshot) {
    console.log(snapshot.val());
  })

This returns null.

However, snapshot has values:

DataSnapshot {
  node_: 
   ChildrenNode {
     children_: 
      SortedMap {
        comparator_: [Function: NAME_COMPARATOR],
        root_: LLRBEmptyNode {} },
     priorityNode_: null,
     indexMap_: IndexMap { indexes_: [Object], indexSet_: [Object] },
     lazyHash_: '' },
  ref_: 
   Reference {
     repo: 
      Repo {
        repoInfo_: [RepoInfo],
        app: [FirebaseApp],
        dataUpdateCount: 2,
        statsListener_: null,
        eventQueue_: [EventQueue],
        nextWriteId_: 3,
        interceptServerDataCallback_: null,
        onDisconnect_: [SparseSnapshotTree],
        persistentConnection_: [PersistentConnection],
        stats_: [StatsCollection],
        server_: [PersistentConnection],
        statsReporter_: [StatsReporter],
        transactionQueueTree_: [Tree],
        infoData_: [SnapshotHolder],
        infoSyncTree_: [SyncTree],
        serverSyncTree_: [SyncTree],
        __database: [Database] },
     path: Path { pieces_: [Array], pieceNum_: 0 },
     queryParams_: 
      QueryParams {
        limitSet_: false,
        startSet_: false,
        startNameSet_: false,
        endSet_: false,
        endNameSet_: false,
        limit_: 0,
        viewFrom_: '',
        indexStartValue_: null,
        indexStartName_: '',
        indexEndValue_: null,
        indexEndName_: '',
        index_: PriorityIndex {} },
     orderByCalled_: false },
  index_: PriorityIndex {} }

Any ideas on direction or how to go about debugging the Firebase object?

I've spent the last day and a half on trying to fix this, tried several different debugging attempts, read most of the StackOverflow threads on this, but to no avail.

Any help would be greatly appreciated!

Upvotes: 1

Views: 2658

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317372

According to the documentation, val() returns null when there's no data at the location of the query:

Depending on the data in a DataSnapshot, the val() method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that the DataSnapshot is empty (contains no data).

The internal contents of the DataSnapshot object are not to be used directly. Use the public API instead.

Upvotes: 4

Related Questions