Firebase按子值搜索 [英] Firebase search by child value
问题描述
我的Firebase数据库中有以下结构:
I have the following structure on my Firebase database:
我想按姓名,姓氏或电子邮件搜索用户,但由于我在上面的级别中有用户密钥,我不知道如何实现这一点。我正在进行管理员会话,因此无法访问用户密钥。
I would like to search for an user by name, last name or email but as I have the user key in the level above I don't know how I can achieve this. I'm doing and administrator session so it wouldn't have access to the user key.
我尝试过:
let usersRef = firebase.database().ref('users');
usersRef.orderByValue().on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
但它带来了数据库中的所有用户。有什么想法吗?
But it brings all the users on the database. Any ideas?
推荐答案
您可以使用 equalTo()
查找任何内容孩子的价值。在你的情况下, name
:
You can use equalTo()
to find any child by value. In your case by name
:
ref.child('users').orderByChild('name').equalTo('John Doe').on("value", function(snapshot) {
console.log(snapshot.val());
snapshot.forEach(function(data) {
console.log(data.key);
});
});
定义 orderByChild()
的目的您要过滤/搜索的字段。 equalTo()
可以接收字符串,int和布尔值。
The purpose of orderByChild()
is define what field you want to filter/search. equalTo()
can receive an string, int and boolean value.
也可以与自动生成的密钥一起使用( pushKey
)。
Also can be used with auto generated keys (pushKey
) too.
您可以找到所有文档这里
这篇关于Firebase按子值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!