简体   繁体   English

如何根据子值从 Firebase 实时数据库中检索数据

[英]How to retrieve data from firebase realtime database based on child value

Hi I am pretty new to Firebase real time database and this is my first project.嗨,我对 Firebase 实时数据库很陌生,这是我的第一个项目。 Sorry if this is a stupid question.对不起,如果这是一个愚蠢的问题。

I am saving my data as follows.我按如下方式保存我的数据。

firebase database structure: firebase 数据库结构:

图片

Now I want to retrieve all parent chat ids on which the student is participating, using the student_id variable.现在我想使用 student_id 变量检索学生参与的所有家长聊天 ID。

I tried as per this SO question and this structure database and retrieve data documentation, but its not retrieving values.我尝试按照这个SO question和这个结构数据库检索数据文档,但它没有检索值。 Anybody have an idea?有人有想法吗?

I would suggest saving the chatroom IDs your students are in in a separate location.我建议将您的学生所在的聊天室 ID 保存在单独的位置。 For example:例如:

Path:
“/users/$uid/chatrooms”
Data:
{
0: 350,
1: 423
}

Thus you could retrieve the chat room ids first before getting the chatroom data.因此,您可以在获取聊天室数据之前先检索聊天室 ID。

import { initializeApp } from “firebase”;
 import { getDatabase, get, set, ref } from “firebase/database”;


const userChatroomIdsRef = ref(db, ‘/users/${uid}/chatrooms‘);
get(userChatroomIdsRef).then(result => {
const chatroomIds = result.val();
if (!(chatroomIds && chatroomIds instanceof Array)) return; // firebase will return null if its an empty array. 
const getChatroomInfoPromises = chatroomIds.map(id => get(ref(db, ‘/chat/${id}/${uid}’)).then(result => result.val());
Promise.all(getChatroomInfoPromises).then(chatroomInfoArray => { YOUR LOGIC HERE });
});

Removing/adding students from/to chatrooms would now be simple as you could just change the array of chatroomIds.从聊天室中删除/添加学生现在变得很简单,因为您只需更改聊天室 ID 的数组即可。

const userChatroomIdsRef = ref(db, ‘/users/${uid}/chatrooms‘);
get(userChatroomIdsRef).then(result => {
const oldIds = result.val();
const newChatroomIds = oldIds.filter(id => id !== ID TO DELETE);
return set(userChatroomIdsRef, newChatroomIds)
});

This is of course assuming that you know the uid of your student_id.这当然是假设你知道你的 student_id 的 uid。 If you do not know what uid each student_id has, you must must store a reference.如果您不知道每个 student_id 有什么 uid,则必须存储一个引用。 I would suggest saving all student info in the “/users/$uid/” directory.我建议将所有学生信息保存在“/users/$uid/”目录中。 Here you could save the studentId so you can programmatically use it.您可以在此处保存 studentId,以便以编程方式使用它。

In all other firebase logic I would try to use the native firebase uid for querying.在所有其他 firebase 逻辑中,我会尝试使用本机 firebase uid 进行查询。 This will make your life easier.这将使您的生活更轻松。

It's always good the keep information organized on the database so your logic is simple.将信息组织在数据库中总是好的,这样您的逻辑就很简单。

Please check my code for syntax errors;请检查我的代码是否有语法错误; I wrote this on an iPhone.这是我在 iPhone 上写的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Firebase 的实时数据库中检索数据? - How to retrieve data from realtime database in Firebase? 如何为Firebase检索实时数据库的子元素 - How to retrieve child element of realtime database for Firebase 如何在基于Latlng的FIREBASE实时数据库中检索数据 - How to retrieve data in FIREBASE realtime database based on Latlng 如何根据 Firebase 实时数据库中的多个随机 Id 从节点检索和组合数据? - How to retrieve and combine data from a node based on multiple random Ids in Firebase Realtime Database? 如何从Firebase实时数据库正确检索值? - How to properly retrieve value from Firebase Realtime Database? 如何从 Firebase 实时数据库中检索特定数据? - How to retrieve Specific Data from Firebase Realtime Database? 如何从 Firebase 实时数据库中检索数据到 mySqlite? - How to retrieve data from Firebase Realtime Database to mySqlite? 如何从 Firebase 实时数据库中检索数据并将其显示在配置文件仪表板中 - How to retrieve data from firebase realtime database and display it in profile dashboard 如何从Android中的Firebase实时数据库中检索数据? - How to retrieve data from Firebase realtime database in Android? 如何从片段中的 Firebase 实时数据库中检索数据? - How to Retrieve Data from Firebase Realtime Database in Fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM