我正在尝试获取数据库中第一个子节点的id,但它返回“undefined”。下面是我的代码,以及我的数据结构:
var database = firebase.database().ref();
var postsRef = database.child("posts");
postsRef.orderByChild("date").limitToFirst(1).once("value", function(snapshot) {
console.log(snapshot.ref.parent);
});
这就是我的数据结构:
posts
1
date
title
alias
timestamp
2
date
title
alias
timestamp
3
date
title
alias
timestamp
答案 0 :(得分:1)
var snapshotKey = snapshot.key;
var snapshotData = snapshot.val();
答案 1 :(得分:1)
var postsRef = database.child("posts");
postsRef.orderByChild("date").limitToFirst(1).once("value", function(snapshot) {
console.log(snapshot.key);
});
https://firebase.google.com/docs/reference/js/firebase.database.Reference#limitToFirst https://firebase.google.com/docs/reference/js/firebase.database.Reference#key