简体   繁体   English

Firebase Web检索数据

[英]Firebase Web retrieve data

I have the following db structure in firebase 我在firebase中有以下db结构

在此输入图像描述

I'm trying to grab data that belongs to a specific user id (uid). 我正在尝试获取属于特定用户ID(uid)的数据。 The documentation has the following example: 该文档具有以下示例:

firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
  var username = snapshot.val().username;
  // ...
});

But how can I retrieve data from my example without knowing the unique key for each object? 但是,如何在不知道每个对象的唯一键的情况下从我的示例中检索数据?

Update: 更新:

I tried a new approach by adding the user id as the main key and each child object has it's own unique id. 我尝试了一种新方法,将用户ID添加为主键,每个子对象都有自己唯一的ID。

在此输入图像描述

Now the challenge is how to get the value of "title". 现在的挑战是如何获得“头衔”的价值。

firebase.database().ref('/tasks/').orderByChild('uid').equalTo(userUID)

Well that is pretty straightforward. 那很简单。 Then you can use it like this: 然后你可以像这样使用它:

return firebase.database().ref('/tasks/').orderByChild('uid').equalTo(userUID).once('value').then(function(snapshot) {
  var username = snapshot.val().username;
  // ...
});

Of course you need to set userUID. 当然你需要设置userUID。

It is query with some filtering. 它是一些过滤查询。 More on Retrieve Data - Firebase doc 有关检索数据的更多信息 - Firebase文档

Edit: Solution for new challenge is: 编辑:新挑战的解决方案是:

var ref = firebase.database().ref('/tasks/' + userUID);
//I am doing a child based listener, but you can use .once('value')...
ref.on('child_added', function(data) {
   //data.key will be like -KPmraap79lz41FpWqLI
   addNewTaskView(data.key, data.val().title);
});

ref.on('child_changed', function(data) {
   updateTaskView(data.key, data.val().title);
});

ref.on('child_removed', function(data) {
   removeTaskView(data.key, data.val().title);
});

Note that this is just an example. 请注意,这只是一个例子。

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

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