[英]Check if data exists in firebase using Ionic
我正在嘗試檢查我的 firebase 實時數據庫中是否存在某個值。 但是,它一直說它不存在。 這是我的代碼
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.");
}
});
}
謝謝
我正在使用離子 5
我認為您正在使用 javascript“hasOwnProperty”方法,而您需要使用 firebase 提供的方法“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
});
所以在你的情況下應該是這樣的:
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.");
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.