Firebase - 按值搜索 child
Firebase - Search a child by value
假设我的 Firebase 上有以下数据:
https://mygame.firebaseio.com/player
{
"player": {
"bloodgulf01": {
"username": "bloodgulf01",
"uid": "twitter:102032",
"level": 2,
"map": 3,
"x": 51,
"y": 12
},
"damjanovic": {
"username": "damjanovic",
"uid": "github:77371",
"level": 5,
"map": 2,
"x": 21,
"y": 44
}
}
}
我如何按 uid
进行搜索并获得结果 snapshot
?
这是我尝试过的:
new Firebase("https://mygame.firebaseio.com/player")
.startAt(authData.uid)
.endAt(authData.uid)
.once('value', function(snap) {
console.log('accounts matching UID of ' + authData.uid, snap.val())
});
returns:accounts matching UID of github:123456789 null
尽管在 /player/
的数据中有 uid
...
记住,Firebase 中的一切都是 url。所以你可以这样做来获得你想要的数据:
'https://mygame.firebaseio.com/player/' + uid
按要筛选的 child 排序,然后筛选:
new Firebase("https://mygame.firebaseio.com/player")
.orderByChild('uid')
.equalTo(authData.uid)
.once('child_added', function(snap) {
console.log('account matching UID of ' + authData.uid, snap.val())
});
由于您只需要一个玩家,因此 once('child_added'
或许可以应付。如果您需要处理具有相同 uid 的潜在多个玩家,则:
.on('child_added', function(snap) {
console.log('account matching UID of ' + authData.uid, snap.val())
});
或
.once('value', function(snap) {
snap.forEach(function(child) {
console.log('account matching UID of ' + authData.uid, child.val())
});
});
虽然我在结构上支持 Chrillewoodz:我 总是 希望看到 uid
作为用户集合的键。那你可以用上面的方法搜索名字。
假设我的 Firebase 上有以下数据:
https://mygame.firebaseio.com/player
{
"player": {
"bloodgulf01": {
"username": "bloodgulf01",
"uid": "twitter:102032",
"level": 2,
"map": 3,
"x": 51,
"y": 12
},
"damjanovic": {
"username": "damjanovic",
"uid": "github:77371",
"level": 5,
"map": 2,
"x": 21,
"y": 44
}
}
}
我如何按 uid
进行搜索并获得结果 snapshot
?
这是我尝试过的:
new Firebase("https://mygame.firebaseio.com/player")
.startAt(authData.uid)
.endAt(authData.uid)
.once('value', function(snap) {
console.log('accounts matching UID of ' + authData.uid, snap.val())
});
returns:accounts matching UID of github:123456789 null
尽管在 /player/
的数据中有 uid
...
记住,Firebase 中的一切都是 url。所以你可以这样做来获得你想要的数据:
'https://mygame.firebaseio.com/player/' + uid
按要筛选的 child 排序,然后筛选:
new Firebase("https://mygame.firebaseio.com/player")
.orderByChild('uid')
.equalTo(authData.uid)
.once('child_added', function(snap) {
console.log('account matching UID of ' + authData.uid, snap.val())
});
由于您只需要一个玩家,因此 once('child_added'
或许可以应付。如果您需要处理具有相同 uid 的潜在多个玩家,则:
.on('child_added', function(snap) {
console.log('account matching UID of ' + authData.uid, snap.val())
});
或
.once('value', function(snap) {
snap.forEach(function(child) {
console.log('account matching UID of ' + authData.uid, child.val())
});
});
虽然我在结构上支持 Chrillewoodz:我 总是 希望看到 uid
作为用户集合的键。那你可以用上面的方法搜索名字。