Firebase数据库快照.val()返回null

时间:2018-08-01 19:36:54

标签: javascript firebase firebase-realtime-database firebase-admin

我正在使用firebase-admin (5.13.1) NodeJS库,无法让.val().on('value')函数上工作。

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

这将返回null

但是,snapshot具有以下值:

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 {} }

关于方向或如何调试Firebase对象的任何想法?

我花了最后一天半的时间来解决此问题,尝试了几次不同的调试尝试,并在此上阅读了大多数StackOverflow线程,但无济于事。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

根据文档,当查询位置没有数据时,val()返回null:

  

根据DataSnapshot中的数据,val()方法可能会返回   标量类型(字符串,数字或布尔值),数组或对象。它   可能还返回null,表明DataSnapshot为空   (不包含任何数据)。

不要直接使用DataSnapshot对象的内部内容。请改用公共API。

相关问题