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 don't 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()
可以获取字符串、整数和布尔值.
The purpose of orderByChild()
is to define the field you want to filter/search for. equalTo()
can get an string, int and boolean value.
也可以与自动生成的密钥 (pushKey
) 一起使用.
Also can be used with auto generated keys (pushKey
) too.
您可以在此处
这篇关于Firebase 按子值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!