2

I have buttons similar to the one here A3. I want to check the availability of the seat from my database and do accordingly in the if loop. But none of my functions below the firebase reference is working.

My database is

My web (app)
     |
    Seats(node)
        |
      Status (node)
           |
          A1 - available (value)
          A2 - available (value)
          A3 - available (value)
          A4 - available (value)

And my js script is

function A3(){
  var newseat = document.getElementById("A3");
  var firebaseStatusRef = firebase.database().ref().child("Status"); 
  var firebaseSeatStatusRef = firebaseStatusRef.child("A3");

  firebaseSeatStatusRef.on('child_added', snap => {
    var seatStatus = snap.val();
  });

if (firebaseSeatStatusRef=="available") {
  newseat.className="booked-seat";
  newseat.setAttribute("class" , "booked-seat");
  firebaseStatusRef.child("A3").set("booked");
  var fill = newseat.value;
document.getElementById("fill").value += fill + " ";
Seat();

} else  {

  newseat.style.backgroundColor = 'seat';
  newseat.setAttribute("class" , "seat");
  var fill = newseat.value;
document.getElementById("fill").value -=  fill + " ";
BookedSeat();

}

}

2
  • firebase.database().ref().child("Status"); Your diagram shows that you have a Seats node above status, so that'd be firebase.database().ref().child("Seats/Status"); Commented Dec 30, 2018 at 7:15
  • Thank you Frank but still nothing works, not even the databse is updated Commented Dec 30, 2018 at 8:34

1 Answer 1

1

Your check boils down to:

var firebaseStatusRef = firebase.database().ref("Seats/Status");
var firebaseSeatStatusRef = firebaseStatusRef.child("A3");
if (firebaseSeatStatusRef=="available") {

This makes no sense in the Firebase Realtime Database API, as you're not reading the value from the database yet. To do this, you'll need to attach a listener, and then check the snapshot that you get against the value. Something like:

firebaseSeatStatusRef.on('value', snap => {
  var seatStatus = snap.val();
  if (seatStatus == "available") {
    ...
  }
});

Note that any code that needs access to the data from the database must be inside the callback block.

This is fairly basic interaction with the Firebase Realtime Database, so you'd do well to study the Firebase documentation on reading values, and possibly taking the Firebase codelab.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.