Firebase:在javascript事件中查询过滤 [英] Firebase: Query filtering in javascript on() events
问题描述
更新:从Firebase 2.0开始,现在根据字段查询。
目前还没有办法从路径中选择数据(即过滤返回的字段)。最简单的解决方案是将数据存储在不同的路径中。这大大简化了这个过程。
用户/
用户/名称/ fred /第一个
个用户/ chat_history / fred / ...
另一个有用的方法,特别是当你要使用很多的子集(例如朋友列表,组的成员等)将使用索引。
user_index / group_1 / fred
user_index / group_1 / mary
user_index / group_2 / ...
users / fred / name
users / fred / chat_history
$ c
$ b $ p
$ b 然后你可以像这样访问数据:
var fb = new Firebase(INSTANCE);
fb.child('user_index')。on('child_added',function(ss){
fb.child('users /'+ ss.name()+'/ name')。once ('value',function(nameSnapshot){
var userName = nameSnapshot.val();
});
});
Is there a way to filter down which properties of each child I want to bring back inside of ref.on("value", ...)?
Let's say I have the structure on https://www.firebase.com/docs/data-structure.html, but in addition to name, each user also has a list of every line of chat he has produced. How can I bring back only the name?
解决方案 UPDATE: As of Firebase 2.0, you can now query based on fields.
There isn't currently a way to pick data out of a path (i.e. filter which fields you get back). The easiest solution here is to store the data in different paths. It greatly simplifies the process.
users/
users/names/fred/first
users/chat_history/fred/...
Another useful approach, particularly when you're going to be using a lot of subsets (e.g. friends list, members of a group, et al) is to use an index.
user_index/group_1/fred
user_index/group_1/mary
user_index/group_2/...
users/fred/name
users/fred/chat_history
Then you can access the data with something like this:
var fb = new Firebase(INSTANCE);
fb.child('user_index').on('child_added', function(ss) {
fb.child('users/'+ss.name()+'/name').once('value', function(nameSnapshot) {
var userName = nameSnapshot.val();
});
});
这篇关于Firebase:在javascript事件中查询过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!