我正在使用Firebase Realtime DB。我正在尝试按特定值(升)查询帖子。每个帖子都有一个随机标识符以及一些属性。
要查询数据库,我在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 }
此输出是怎么回事,为什么我没有得到我的数据?
答案 0 :(得分:1)
您的代码尚未读取数据。相反,bestPosts = dbRef.orderByChild('ups')
只是创建一个查询,然后可以通过向其附加侦听器来执行该查询。
bestPosts.once("value").then(function(snapshot) {
console.log(snapshot.val());
})
我强烈建议您阅读reading data from the database上的Firebase文档,因为这样可以节省大量时间。