最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase Web retrieve data - Stack Overflow

programmeradmin6浏览0评论

I have the following db structure in firebase

I'm trying to grab data that belongs to a specific user 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.

Now the challenge is how to get the value of "title".

I have the following db structure in firebase

I'm trying to grab data that belongs to a specific user 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.

Now the challenge is how to get the value of "title".

Share Improve this question edited Aug 22, 2016 at 16:02 CyberJunkie asked Aug 22, 2016 at 15:19 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 14
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.

It is query with some filtering. More on Retrieve Data - Firebase doc

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.

发布评论

评论列表(0)

  1. 暂无评论