[英]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节点中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.