Isaac Meneses
Isaac Meneses

Reputation: 163

Acess node firebase and check value

enter image description here

I would like to access the node "status" and check: if false writes data, if true returns error alert.

I used the code:

var reference_value_of_counter = firebase.database().ref('contador/valor');

var reference_to_increment_counter = firebase.database().ref('contador/valor');

var reference_status_attendance = firebase.database().ref('agendamentos/primeiro-horario/status');

var reference_to_data_register = firebase.database().ref('agendamentos/primeiro-horario/');


if (reference_status_attendance.once('value', snap => snap.val() == false)) {

    reference_value_of_counter.once('value', snap => reference_to_increment_counter.set(snap.val()+1));

    reference_to_data_register.update({nome : 'isaac', sobrenome : "menezes", status: true});

}

else {
    alert("sorry");
}

Upvotes: 2

Views: 109

Answers (2)

Ronnie Smith
Ronnie Smith

Reputation: 18565

See Firebase Documentation

var reference_to_data_register = firebase.database().ref('agendamentos/primeiro-horario/');

reference_to_data_register.once('value')
  .then(function(dataSnapshot) {
    if(dataSnapshot.val().status){
        //do this if status is true
    } else {
        // do this is status is false
    }
  });

Upvotes: 0

Varun Gupta
Varun Gupta

Reputation: 3112

if (reference_status_attendance.once('value', snap => snap.val() == false))

This statement is not correct. once function wouldn't return the value that you return from your callback. You need to access the value and perform the if/else inside the callback. I haven't tested the below code but it should work or at least give you an idea of what needs to be done.

reference_status_attendance.once('value', snap => { 
  if(snap.val() == false) {

    reference_value_of_counter.once('value', snap => reference_to_increment_counter.set(snap.val()+1));

    reference_to_data_register.update({nome : 'isaac', sobrenome : "menezes", status: true});

  }

  else {
    alert("sorry");
  }
})

Upvotes: 1

Related Questions