Reputation: 41
I am trying to check if a certain value exists in my firebase real time database. However, it keeps saying that it does not exist. This is my code
check(){
var checkRef = firebase.database().ref('users');
checkRef.once('value', function(snapshot) {
if(snapshot.val().hasOwnProperty('referralCode')) {
console.log("Referral Code exist.");
}
else {
console.log("Referral Code does not exist.");
}
});
}
Thanks
I am using Ionic 5
Upvotes: 0
Views: 176
Reputation: 9227
I think you are using javascript "hasOwnProperty" method while you need to use firebase provided method "hasChild":
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
// Determine which child keys in DataSnapshot have data.
var ref = firebase.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var hasName = snapshot.hasChild("name"); // true
var hasAge = snapshot.hasChild("age"); // false
});
So in your case should be something like:
check(){
var checkRef = firebase.database().ref('users');
checkRef.once('value', function(snapshot) {
if(snapshot.hasChild('referralCode')) {
console.log("Referral Code exist.");
}
else {
console.log("Referral Code does not exist.");
}
});
}
Upvotes: 1