javascript - Firebase DB snapshot.val() 返回 null

标签 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 线程,但没有成功。

任何帮助将不胜感激!

最佳答案

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

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).

DataSnapshot 对象的内部内容不能直接使用。请改用公共(public) API。

关于javascript - Firebase DB snapshot.val() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641097/

相关文章:

ios - Firebase 使用电子邮件、密码、显示名称和照片网址创建用户

php - "registration_ids"字段不是 JSON 数组 (Firebase)

ios - 调用 Firebase Observer 后重新加载 TableView

javascript - 测试值是否与 Firebase 数据库相同

javascript - 使用新用户输入重复通过 Firebase 显示的数据

javascript - 如果某些子节点没有标签,如何更改子节点的 innerHTML?

javascript - 将属性添加到复选框背景网格

javascript - Prop `loadingElement` 在 `withScriptjs(withGoogleMap(Component)) 中被标记为必需

Javascript - 如何扩展此脚本以合并另一个类更改?

swift - 如何让用户保持登录状态? swift