简体   繁体   English

Firebase如何找到知道其ID但不知道其父母ID(JS)的孩子

[英]Firebase how to find child knowing its id but not its parent's id (js)

I have a firebase realtime database (not cloud) structure like: 我有一个Firebase实时数据库(不是云)结构,例如:

users
  kasdf872ajsda   //user id
     auiiq6d182g1   //list id
        c: <mycontent>   //list content

i know the list id (in this case auiiq6d182g1 ) and i want to retrieve <mycontent> , but i don't know the user id kasdf872ajsda , because what i'm going to retrieve is probably not from the user currently using the website (and i'm not setting any database rules for "read" in fact, only for "write" is that correct?). 我知道列表ID(在本例中为auiiq6d182g1 ),我想检索<mycontent> ,但我不知道用户ID kasdf872ajsda ,因为我要检索的内容可能不是当前使用该网站的用户(并且我实际上没有为“读取”设置任何数据库规则,仅针对“写入”是正确的吗?)。

What i'm doing right now is this (not working): 我现在正在做的是这样(不起作用):

var ref = firebase.database().ref().child('users');
ref.child(listID).once("value", function(snapshot) {
  var snap = snapshot.val();
  myContent = snap.c;
});

You could put your reference to users first: var ref = firebase.database().ref('users'); 您可以var ref = firebase.database().ref('users');用户的引用放在首位: var ref = firebase.database().ref('users');

Then loop through: 然后循环:

ref.once('value').then((snapshot)=>{
   snapshot.forEach(function(data) {
      if (data.key == <YOUR_LIST_ID>) {
         //you can access data.c here...
      }
  });
});  

Found the solution, if someone should encounter the same issue: 找到解决方案,如果有人应该遇到相同的问题:

ref.once("value", function(snapshot) {
      snapshot.forEach(function(data) {
        snap = data.child(<YOUR_LIST_ID>).child('c').val();
        if (snap != null){
          myContent = snap;
        }
      });
});

also ref contrary to what everyone says has to be: ref相反的是大家说必须是:

var ref = firebase.database().ref().child('users');

this doesn't work: 这不起作用:

var ref = firebase.database().ref('users');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM