Fetching Data - simple

213 views
Skip to first unread message

Marcio Valenzuela

unread,
Sep 14, 2016, 9:13:56 PM9/14/16
to Firebase Google Group
I've taken the login sample from the firebase site:


and I've used it to log into my app.  Now I want to move on to fetching data from my app.  So I added this function:

function getMyData() {
var msgName = document.getElementById('name').value;
var newClientKey = database.ref().child('messages').push().key;
database.ref('messages/' + newClientKey + '/name').get(msgName);
window.alert(username);
//document.write(starCountRef);
}

which I plan to call right at the end of the sendPasswordResetEmail function.

I made up that .get() actually because I saw a set() method.  I was wondering if it exists.

The other way I've seen to retrieve data is by using either:

// Try to read a message
// var messageRef = firebase.database().ref('messages/');
// messageRef.on('value', function(snapshot) {
//   updateStarCount(postElement, snapshot.val());
// });

or:
//Try to read users data-once
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) {
  var name = snapshot.val().name;
  // ...
});

My data looks like this:




ry...@bloomcu.com

unread,
Sep 14, 2016, 11:50:43 PM9/14/16
to Firebase Google Group
Hey Marcio,

I recently got over the hump of learning how to read my Firebase data, the two Realtime Database videos on Firebase's Youtube channel made things extremely clear. I really hope this helps you. I'm happy to post my code and help you more. Here are the videos:


Marcio Valenzuela

unread,
Sep 14, 2016, 11:51:00 PM9/14/16
to Firebase Google Group
I've got the first level nodes working, grocery-items and online.

I cannot get the autogenerated id sublevel nodes to work like messages----autokey----name,text...

Ive got this so far.  I know I have to somehow get the key by pushing it and then use it to rebuild the ref:

function getMyData() {
   //1 code for getting messages because of auto-generated id
var messageRef = firebase.database().ref('messages/');
messageRef.forEach(function(secondsnapshot) {
var newClientKey = database.ref().child('messages').push().key;
var textname = database.ref('messages/' + newClientKey + '/name').getValue(msgName);
  window.alert(textname);
  });
}



On Wednesday, September 14, 2016 at 7:13:56 PM UTC-6, Marcio Valenzuela wrote:

Marcio Valenzuela

unread,
Sep 15, 2016, 11:30:12 AM9/15/16
to Firebase Google Group
Ryan

Thanks for the help!  I went through the videos and I am able to get some values great!  Im using this code:

        const preObject = document.getElementById('object');
const dbRefObject = firebase.database().ref().child('timeoff');
dbRefObject.on('value', function(snapshot) {
console.log(snapshot.val());
});

For this data structure I can get values for the first three nodes that have children, but I cant get the values for timeoff or timeon:

Marcio Valenzuela

unread,
Sep 15, 2016, 11:31:01 AM9/15/16
to Firebase Google Group
Thanks Ryan.  Ill look these over.  For the time being, I managed to get the data I needed but by looping through with for loop if child had a key.  Id like to eventually learn how to actually get the key value.

Then I went ahead and complicated things a bit more by trying to access a parent value (timeoff & timeon):



Thanks for the videos.  Ill check them out.

ry...@bloomcu.com

unread,
Sep 18, 2016, 11:41:12 AM9/18/16
to Firebase Google Group
It's very easy to access children nodes by using snapshot, take a look below!

Here I want to access all hobbies:

Here is how I get that data from Firebase by taking a snapshot of the "hobbies" snapshot:



var hobbieList = document.getElementById('hobbieList');
objectRef = firebaseRoot.child('object');

objectRef.child('hobbies').on('child_added', function (snap) {
  var li = document.createElement('li');
  li.innerText = snap.val();
  li.id = snap.key;
  hobbieList.appendChild(li);
});


What I get returned is a list of hobbies, which I place in an unordered list.


I hope this helps you! Let me know, please :)




On Thursday, September 15, 2016 at 9:31:01 AM UTC-6, Marcio Valenzuela wrote:
Thanks Ryan.  Ill look these over.  For the time being, I managed to get the data I needed but by looping through with for loop if child had a key.  Id like to eventually learn how to actually get the key value.

Then I went ahead and complicated things a bit more by trying to access a parent value (timeoff & timeon): 


Enter code here...


Marcio Valenzuela

unread,
Sep 18, 2016, 11:41:47 AM9/18/16
to Firebase Google Group
I thought this code should work:

console.log("testing");
return firebase.database().ref('timeoff').once('value', function(snapshot){   
      console.log("hello");
      window.alert(snapshot.val());
})


but it doesnt.

Any ideas?



On Thursday, September 15, 2016 at 9:31:01 AM UTC-6, Marcio Valenzuela wrote:

Marcio Valenzuela

unread,
Sep 19, 2016, 12:11:42 AM9/19/16
to Firebase Google Group
Yes I see that. But those are all children of the object object.

How would you access the title object's value of "The Title"?

Marcio Valenzuela

unread,
Sep 19, 2016, 11:16:21 PM9/19/16
to Firebase Google Group
Turned out to be a permissions issue.  My code was fine.

Ryan Harmon

unread,
Sep 20, 2016, 12:17:28 AM9/20/16
to fireba...@googlegroups.com
D'oohhh why didn't I think about that! Yeah, until you setup anonymous auth, or email auth you have to set your database read/write permissions to true.

Sorry I didn't think of that, glad you figured it out!

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/e2NByQCW-NA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/5833a89e-7be2-4a55-b24a-c325f30a6cbf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Ryan Harmon
Co-Founder | bloomcu.com
Reply all
Reply to author
Forward
0 new messages