【问题标题】:Firebase Database Query Prints Database Info, Not DataFirebase 数据库查询打印数据库信息,而不是数据
【发布时间】:2020-12-11 09:49:13
【问题描述】:

我正在使用 Firebase 实时数据库。我正在尝试按特定值(ups)查询帖子。每个帖子都有一个随机标识符以及许多属性。

为了查询数据库,我在 index.js 文档中运行了以下代码:

  var firebase = require("firebase");
    firebase.initializeApp({
      databaseURL: "<URL>"
    });
    var dbRef = firebase.database().ref('posts/rice/content');
    var bestPosts = dbRef.orderByChild('ups');
    console.log(bestPosts)

但它没有打印出数据,而是打印出一堆关于我的查询本身的信息。

Query {   repo: <ref *1> Repo {
    repoInfo_: RepoInfo {
      secure: true,
      namespace: 'librexreacttester',
      webSocketOnly: false,
      persistenceKey: '',
      includeNamespaceInQueryParams: false,
      host: 'librexreacttester.firebaseio.com',
      domain: 'firebaseio.com',
      internalHost: 'librexreacttester.firebaseio.com'
    },
    app: FirebaseAppImpl {
      firebase_: [Object],
      isDeleted_: false,
      name_: '[DEFAULT]',
      automaticDataCollectionEnabled_: false,
      options_: [Object],
      container: [ComponentContainer]
    },
    dataUpdateCount: 0,
    statsListener_: null,
    eventQueue_: EventQueue { eventLists_: [], recursionDepth_: 0 },
    nextWriteId_: 1,
    interceptServerDataCallback_: null,
    onDisconnect_: SparseSnapshotTree { value: null, children: Map(0) {} },
    persistentConnection_: PersistentConnection {
      repoInfo_: [RepoInfo],
      applicationId_: undefined,
      onDataUpdate_: [Function: bound ],
      onConnectStatus_: [Function: bound ],
      onServerInfoUpdate_: [Function: bound ],
      authTokenProvider_: [FirebaseAuthTokenProvider],
      authOverride_: undefined,
      id: 0,
      log_: [Function (anonymous)],
      interruptReasons_: {},
      listens: Map(0) {},
      outstandingPuts_: [],
      outstandingPutCount_: 0,
      onDisconnectRequestQueue_: [],
      connected_: false,
      reconnectDelay_: 1000,
      maxReconnectDelay_: 300000,
      securityDebugCallback_: null,
      lastSessionId: null,
      establishConnectionTimer_: Timeout {
        _idleTimeout: 1,
        _idlePrev: [Timeout],
        _idleNext: [TimersList],
        _idleStart: 259,
        _onTimeout: [Function (anonymous)],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: true,
        [Symbol(asyncId)]: 4,
        [Symbol(triggerId)]: 1
      },
      visible_: true,
      requestCBHash_: {},
      requestNumber_: 0,
      realtime_: null,
      authToken_: null,
      forceTokenRefresh_: false,
      invalidAuthTokenCount_: 0,
      firstConnection_: true,
      lastConnectionAttemptTime_: null,
      lastConnectionEstablishedTime_: null
    },
    stats_: StatsCollection { counters_: {} },
    server_: PersistentConnection {
      repoInfo_: [RepoInfo],
      applicationId_: undefined,
      onDataUpdate_: [Function: bound ],
      onConnectStatus_: [Function: bound ],
      onServerInfoUpdate_: [Function: bound ],
      authTokenProvider_: [FirebaseAuthTokenProvider],
      authOverride_: undefined,
      id: 0,
      log_: [Function (anonymous)],
      interruptReasons_: {},
      listens: Map(0) {},
      outstandingPuts_: [],
      outstandingPutCount_: 0,
      onDisconnectRequestQueue_: [],
      connected_: false,
      reconnectDelay_: 1000,
      maxReconnectDelay_: 300000,
      securityDebugCallback_: null,
      lastSessionId: null,
      establishConnectionTimer_: Timeout {
        _idleTimeout: 1,
        _idlePrev: [Timeout],
        _idleNext: [TimersList],
        _idleStart: 259,
        _onTimeout: [Function (anonymous)],
        _timerArgs: undefined,
        _repeat: null,
        _destroyed: false,
        [Symbol(refed)]: true,
        [Symbol(asyncId)]: 4,
        [Symbol(triggerId)]: 1
      },
      visible_: true,
      requestCBHash_: {},
      requestNumber_: 0,
      realtime_: null,
      authToken_: null,
      forceTokenRefresh_: false,
      invalidAuthTokenCount_: 0,
      firstConnection_: true,
      lastConnectionAttemptTime_: null,
      lastConnectionEstablishedTime_: null
    },
    statsReporter_: StatsReporter {
      server_: [PersistentConnection],
      statsToReport_: {},
      statsListener_: [StatsListener]
    },
    transactionQueueTree_: Tree { name_: '', parent_: null, node_: [TreeNode] },
    infoData_: SnapshotHolder { rootNode_: [ChildrenNode] },
    infoSyncTree_: SyncTree {
      listenProvider_: [Object],
      syncPointTree_: [ImmutableTree],
      pendingWriteTree_: [WriteTree],
      tagToQueryMap: Map(0) {},
      queryToTagMap: Map(0) {}
    },
    serverSyncTree_: SyncTree {
      listenProvider_: [Object],
      syncPointTree_: [ImmutableTree],
      pendingWriteTree_: [WriteTree],
      tagToQueryMap: Map(0) {},
      queryToTagMap: Map(0) {}
    },
    __database: Database {
      repo_: [Circular *1],
      root_: [Reference],
      INTERNAL: [DatabaseInternals]
    }   },   path: Path { pieces_: [ 'posts', 'rice', 'content' ], pieceNum_: 0 },   queryParams_: QueryParams {
    limitSet_: false,
    startSet_: false,
    startNameSet_: false,
    endSet_: false,
    endNameSet_: false,
    limit_: 0,
    viewFrom_: '',
    indexStartValue_: null,
    indexStartName_: '',
    indexEndValue_: null,
    indexEndName_: '',
    index_: PathIndex { indexPath_: [Path] }   },   orderByCalled_: true }

这个输出是怎么回事,为什么我没有得到我的数据?

【问题讨论】:

    标签: javascript node.js firebase firebase-realtime-database nosql


    【解决方案1】:

    您的代码尚未读取数据。相反,bestPosts = dbRef.orderByChild('ups') 只是创建一个查询,然后您可以通过将侦听器附加到它来执行该查询。

    bestPosts.once("value").then(function(snapshot) {
      console.log(snapshot.val());
    })
    

    我强烈建议您阅读 reading data from the database 上的 Firebase 文档,因为在那里几个小时可以为您节省大量时间。

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2019-07-28
      • 1970-01-01
      相关资源
      最近更新 更多