如果数据不存在,Firebase 查询不会显示任何数据
Firebase query dont display any data if data dont exist
如果数据不存在,Firebase 查询不显示任何数据 no null no undefined , nothing no message at all, it only shows if it find something in the database.
const ref = this.fb.database().ref("Users");
var self = this;
ref.orderByChild('cellphone').equalTo('+351' + this.phoneNumber.toString()).on("child_added", (snapshot) => {
console.log(snapshot.val().cellphone);
if (snapshot.exists()){
let toast = self.toastCtrl.create({
message: 'Cellphone already exist',
duration: 2000,
position: 'bottom'
});
toast.present();
}
if(!snapshot.exists()){ // it dont even get here
console.log('can register');
}
});
我的数据库结构,所以如果它没有找到我的手机根本没有拍摄任何东西
解决方案是 using "value" instead of "child_added"
child_added
事件针对您查询的节点下的每个匹配 child 触发。如果没有匹配的children,则不会触发。
这意味着要测试节点是否存在,您必须使用value
处理程序。由于一个查询可以有多个结果,因此您需要遍历 children.
如果数据不存在,Firebase 查询不显示任何数据 no null no undefined , nothing no message at all, it only shows if it find something in the database.
const ref = this.fb.database().ref("Users");
var self = this;
ref.orderByChild('cellphone').equalTo('+351' + this.phoneNumber.toString()).on("child_added", (snapshot) => {
console.log(snapshot.val().cellphone);
if (snapshot.exists()){
let toast = self.toastCtrl.create({
message: 'Cellphone already exist',
duration: 2000,
position: 'bottom'
});
toast.present();
}
if(!snapshot.exists()){ // it dont even get here
console.log('can register');
}
});
我的数据库结构,所以如果它没有找到我的手机根本没有拍摄任何东西
解决方案是 using "value" instead of "child_added"
child_added
事件针对您查询的节点下的每个匹配 child 触发。如果没有匹配的children,则不会触发。
这意味着要测试节点是否存在,您必须使用value
处理程序。由于一个查询可以有多个结果,因此您需要遍历 children.