使用错误数据进行的Firebase查询不会提供任何回电 [英] Firebase query with wrong data doesn't give any call back
问题描述
我已经在firebase中创建了一个表,并保存了一些数据,并希望通过指定输入来查询特定的数据,例如Sqlite中的where条件:
Query queryRef = ref.orderByChild(name)。equalTo(username);
queryRef.addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String previousChild){
//我接到回电if我在表
}
}}中指定了已经存在的用户名;
我的问题是当我传递任何不存在于表中的值时,任何回电。
如何在Firebase中处理这种情况。有什么其他的回调,我应该听吗?
addValueEventListener( )
。您可以在其他类似问题上找到更多详情。 。 但是你的代码如下所示,你可以使用 snapshot.exists() / code>。
查询queryRef = ref.orderByChild(name)。equalTo(username);
queryRef.addValueEventListener(new ValueEventListener(){
$ b @Override
public void onDataChanged(DataSnapshot snapshot){
if(snapshot.exists() ){
//找到结果
}
else {
//找不到
}
}
});
I have created a table in firebase and saved couple of data and want to query specific data by specifying inputs like where condition in Sqlite :
Query queryRef = ref.orderByChild("name").equalTo(username);
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChild) {
// i get the call back if i specify username which is already there in table
}
});
my problem is when i pass any value which doesn't exist in table then i don't get any call back.
how to handle such scenario in Firebase. is there any other call back which i should be listening to ?
You should be using addValueEventListener()
. You can find more details on this other similar question.
But your code will look like the following and you will be able to see if there is some matching results using snapshot.exists()
.
Query queryRef = ref.orderByChild("name").equalTo(username);
queryRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChanged(DataSnapshot snapshot) {
if (snapshot.exists()) {
//found results
}
else {
//not found
}
}
});
这篇关于使用错误数据进行的Firebase查询不会提供任何回电的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!