Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having issues reading from the database #59

Closed
jalasem opened this issue Sep 3, 2016 · 3 comments
Closed

Having issues reading from the database #59

jalasem opened this issue Sep 3, 2016 · 3 comments

Comments

@jalasem
Copy link

jalasem commented Sep 3, 2016

I am building a blog powered by firebase just to try it out on my own. But issues looping through my posts. this code :

firebase.database().ref('blog/').child('posts/')on('value', function(snap){
      console.log(snap.val());
});

and 

firebase.database().ref('blog/').child('posts/').on("value", snap => {
      for (let post in snap.val()) {
          console.log(post)
      }
});

returns objects with probably the post ids of those objects which I dont know how to determine that.
My question is Do I have to work with postId?
If yes, how? and if no, How?

@nicolasgarnier
Copy link
Contributor

nicolasgarnier commented Sep 4, 2016

Hey @jalasem

This will trigger once per post, and then keep listening for new incoming posts (see the child_changed instead of value):

firebase.database().ref('blog/').child('posts/')on('child_changed', function(snap){
      console.log(snap.val()); // value of the post
      console.log(snap.key); // ID of the post
});

Alternatively you can use once which will trigger just once and in this csae return all blog posts. but you won't be notified of new posts:

firebase.database().ref('blog/').child('posts/').once('value').then(function(snap){
  snap.foreach(function(posts){
      console.log(posts.val()); // value of the post
      console.log(posts.key); // ID of the post
  });
});

@jalasem
Copy link
Author

jalasem commented Sep 4, 2016

Thanks a lot! You are awesome. Where can I find this in the documentation, because the documentation the way I see it is more or less of a tutorial on how to build a chat app.
I suggest firebase web documentation should be more explanatory

@jalasem jalasem closed this as completed Sep 4, 2016
@jalasem jalasem reopened this Sep 4, 2016
@nicolasgarnier
Copy link
Contributor

Here is the relevant doc: https://firebase.google.com/docs/database/web/retrieve-data
Especially see the Listen for events section which described every type of event you can listen to and the Read data once section which explains how to use the .once.

We're indeed using a chat app as a way to illustrate the docs by showing an example you can follow. If you have different ideas, feedback or see any error in the docs we'd love to hear about it. You can use the "Send Feedback" button in the docs and this will go directly to out tech writer team:
image

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants