Erick2280
Erick2280

Reputation: 300

Test if value is the same of Firebase database

I have a realtime database in a Firebase project, with a registered value. In a minigame, the device reads a QR-Code and store the information. So I want to send the value to Firebase, to the server test if value is the same of the database, and return just "true" or "false".

I can do this, but I get the value to the device and I perform the test in the client. My goal is just send the value and the server will perform the test.

I'm a newbie in Firebase, and I'm not sure if I can do this.

Upvotes: 0

Views: 69

Answers (1)

Matt T.
Matt T.

Reputation: 144

Assuming you have already linked Firebase to your app, you first need to make two references to your database:

var database = firebase.database(); //This links to your database

and

var nodeRef = firebase.database().ref("value/").child("IF APPLICABLE"); //This references to the node you want to compare.

Notice .child(). If the node you want is nested inside another node, you have to use .child("NODE HERE") to reference it.

Next to read the value, you would use:

nodeRef.on('value', function(snapshot) {
    nodeValue = snapshot.val();
    if (nodeValue == comparisonValue) {
        //Do something
});

This calls on your reference and gets its value in your database. Then, within the if statement, you can execute the code that you want. You just have to make sure that they use the same word before the ".on".

Please ask if you need any more clarification!

Upvotes: 3

Related Questions