NotToBrag
NotToBrag

Reputation: 665

Firebase checking to see if data exists

I have a list of random codes with their respective values in my database as follows:

   123
   |----- value : 1
   877
   |----- value : 2
   245
   |----- value : 1

I want the user to input a value and have Firebase check to see if the code he entered matches any in the database. If it does, it should alert the value of the matched code.

I came up with the following code:

var inputCode = "877"; // user input value
checkCode(inputCode);

function checkCode(inputCode) {
  var ref = new Firebase("https://xxx.firebaseio.com");
  ref.child(inputCode).on('child_added', function(snapshot) {
    alert(snapshot.value);
  });
}

I'm not sure why this isn't working, any suggestions?

Upvotes: 0

Views: 367

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599071

Works for me, although the correct syntax is snapshot.val().

var inputCode = "877"; // user input value
checkCode(inputCode);

function checkCode(inputCode) {
  ref.child(inputCode).on('child_added', function(snapshot) {
    console.log(snapshot.val());
  });
}

Working JSBin: http://jsbin.com/fakuha/edit?js,console

Upvotes: 2

Related Questions