简体   繁体   English

如何在 firebase 实时数据库中使用子密钥查找父密钥?

[英]How to find parent key using child key in firebase realtime-database?

In firebase realtime-database, my project have a structure like below:在 firebase 实时数据库中,我的项目结构如下:

- posts
    - user_id
        - post_id
            - post_content = "..."
            - date = ...
            ...

How could i find user_id by post_id?我如何通过 post_id 找到 user_id?

I have searched that before but i could not find an appropriate solution for my case.我之前搜索过,但找不到适合我的案例的解决方案。 If there is any i missed out, please report me and i would be thankful for you.如果有任何遗漏,请报告我,我将不胜感激。

I have tried to use orderByKey() and orderByChild() methods but they did not helped me to handle this problem.我曾尝试使用 orderByKey() 和 orderByChild() 方法,但它们并没有帮助我处理这个问题。 At the end, i was able to find the wanted user_id using a logic below:最后,我能够使用以下逻辑找到想要的 user_id:

firebase.database().ref("posts")
    .once("value", function(snapshot) {
        snapshot.forEach(function(snapshot1) {
            snapshot1.forEach(function(snapshot2) {
                if (snapshot2.toJSON().postId === "post_id") {
                    var topicID = snapshot1.key;
                    console.log(topicID);
                }
            });
        });
}, (e) => {
    console.log(e);
});

As it is seen, that is not a best way to find user_id because the method loads lots of data in the client side.正如所见,这不是查找 user_id 的最佳方法,因为该方法会在客户端加载大量数据。

So, how can i find user_id using firebase query methods or any other method, when i only know post_id?那么,当我只知道 post_id 时,如何使用 firebase 查询方法或任何其他方法找到 user_id?

One work around can be you can save user_id in post_id node一种解决方法是您可以将user_id保存在post_id节点中

  • posts帖子
    • user_id用户身份
      • post_id post_id
        • post_content = "..." post_content = "..."
        • date =...日期 =...
        • user_id = #id... user_id = #id...

暂无
暂无

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

相关问题 哪个是使用 firebase 实时数据库更好的性能查询 - Which is a better query for performance using firebase realtime-database 如何用key删除firebase实时数据库子节点? - how to delete firebase realtime database child node with key? 如何在 Flutter 应用程序中以离线模式使用 firebase 实时数据库? - How to use firebase realtime-database in offline mode in Flutter app? 如何从实时数据库(Firebase)中获取 json 格式的数据? - How to fetch data in json format from realtime-database (Firebase)? Firebase Realtime Database随机选择子节点后如何获取子节点的key - How to get the key of a child node in Firebase Realtime Database after choosing a child randomly 如何更新 firebase 实时数据库 (JavaScript) 中键的值 - How to update the value of a key in firebase Realtime database (JavaScript) 如何在 React 中将 Firebase 实时数据库中的数据写入为 [自定义键:值]? - How to write data in Firebase Realtime Database as [custom key : value] in React? Firebase 实时数据库查询最近 10 条记录使用时间戳键 - Firebase realtime database query latest 10 records using timestamp key 如何在 Firebase 实时数据库规则中将键解析为数字? - How to parse key as a number in a Firebase Realtime Database rule? (Firebase Realtime Database| React) 如何在没有密钥的情况下将数据添加到 firebase 实时数据库? - (Firebase Realtime Database| React) How to add data to a firebase realtime database without key?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM