0

I want to retrieve the data from my firebase database.

enter image description here

The one that was highlighted in the IMG 1 is the user uid and below it was the value from the push().

This is my code to retrieve the data

var getClassInfo = firebase.database().ref().child('Classes'+ user.uid + 
'/');
 getClassInfo.on('value', gotData,errData);

function gotData(data) {
//console.log(data.val());
var Classes = data.val()
var keys = Object.keys(Classes);
console.log(keys);


for (var i =0; i < keys.length; i ++){
  var k = keys[i];
  var TheClass = Classes[k].TheClass;
  console.log(TheClass);
  }
  }

function errData(err) {
console.log('Error!');
console.log(err);

 }

How do I ref() the value of the push()?

var getClassInfo = firebase.database().ref().child('Classes'+ user.uid + 
[push()]);

How do I retrieve all classroom name from a particular user [TheClass]?

KENdi
  • 7,576
  • 2
  • 16
  • 31

1 Answers1

0

To get the value of the classroom created via the push, do as follows, where classId is the key created by the push (e.g. LMpv.....)

    firebase.database().ref('Classes/' + user.uid + '/' + classId).once('value').then(function (snapshot) {           
        console.log(snapshot.val().TheClass);
        // ....
    });

To retrieve all the classroom names for a particular user, do as follows:

var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
userRef.once('value', function(snapshot) {
  snapshot.forEach(function(childSnapshot) {
    var childData = childSnapshot.val();
    var classroomName = childData.TheClass
  });
});

By using the once() method, you read once the values from the database, see https://firebase.google.com/docs/reference/js/firebase.database.Reference#once

If you want to use the on() method (as you do in your question) and continuously listen to the user node and detect when new classrooms are added, do as follows:

var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
userRef.on('child_added', function(data) {
  console.log(data.val().TheClass);
});

See the doc here and here.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Thank you so much, Mr. Renaud. One last thing, how do I put it on a list?Let say, I want to show all the classrooms of a particular user on a list in my main.html – Dodong Advices Sep 20 '18 at 15:54
  • You populate your list in the snapshot.forEach() method. See the following for indications on how to do that: https://www.vitoshacademy.com/javascript-create-ul-and-li-elements-with-js-with-chained-function/, https://getbutterfly.com/generate-html-list-from-javascript-array/ or https://stackoverflow.com/questions/11128700/create-a-ul-and-fill-it-based-on-a-passed-array – Renaud Tarnec Sep 20 '18 at 15:57
  • Done!! It says it was recorded – Dodong Advices Sep 21 '18 at 02:31
  • Mr. Renaud, I successfully populated my list. The user can now view all the classrooms he/she created. But I have some questions, I hope that you will still help me. I am very sorry for all the trouble. – Dodong Advices Sep 21 '18 at 11:54
  • @DodongAdvices I would suggest that you create a new question. Don't worry there is no "trouble" at all to help fellow programmers on Stack Overflow :-) We all need some help one day or the other to learn new techniques! – Renaud Tarnec Sep 21 '18 at 12:09
  • I'm considering myself very lucky because you're here, Mr. Renaud. #NoHomo... here's the to my [Link] (https://stackoverflow.com/questions/52443774/how-to-add-links-in-every-data-of-my-list) to my new question – Dodong Advices Sep 21 '18 at 12:28