firebase database equalTo(null) doesn't work as expected on Android
Issue
I'm trying to retrieve a data from firebase real-time database when the value is equal to null, however, the equalTo(null) basically doesn't perform as expected, instead, it returns everything, for example:
Database:
{
"collections": {
"item-1": {
"name": "item-1",
"mark": true
},
"item-2": {
"name": "item-2"
},
"item-3": {
"name": "item-3"
},
"item-4": {
"name": "item-4",
"mark": true
}
}
}
Query code
` const ref = firebase.database().ref('collections') .orderByChild('mark').equalTo(null);
ref.once('value', snapshot => { snapshot.forEach(cs => console.log(cs.key)); }); `
Expected result item-2 item-3
Actual result item-1 item-2 item-3 item-4
As you can see, it just returns the entire list instead. I tested on both IOS & Android, and only the Android will have this issue.
Hey! I'm trying to implement firebase database to my react-native app but database listeners stop working after a few reloads. Could you tell me your RN and RN Firebase versions and settings?
Hi, I found solution:
Go to node_modules-> react-native-firebase -> android -> ... -> firebase -> database -> RNFirebaseDatabaseReference.java
Change method applyEqualToFilter to:
private void applyEqualToFilter(String key, String valueType, Map modifier) {
if ("number".equals(valueType)) {
double value = (Double) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("boolean".equals(valueType)) {
boolean value = (Boolean) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("string".equals(valueType)) {
String value = (String) modifier.get("value");
if (key == null) {
query = query.equalTo(value);
} else {
query = query.equalTo(value, key);
}
} else if ("object".equals(valueType)) {
if (key == null) {
query = query.equalTo(null);
} else {
query = query.equalTo(null, key);
}
}
}
You should propose a PR @Wojew - if it works well others might need it
Hello 👋, this issue has been automatically marked as stale because it has not had activity for quite some time. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.
Hi, I found solution:
Go to node_modules-> react-native-firebase -> android -> ... -> firebase -> database -> RNFirebaseDatabaseReference.java
Change method applyEqualToFilter to:
private void applyEqualToFilter(String key, String valueType, Map modifier) { if ("number".equals(valueType)) { double value = (Double) modifier.get("value"); if (key == null) { query = query.equalTo(value); } else { query = query.equalTo(value, key); } } else if ("boolean".equals(valueType)) { boolean value = (Boolean) modifier.get("value"); if (key == null) { query = query.equalTo(value); } else { query = query.equalTo(value, key); } } else if ("string".equals(valueType)) { String value = (String) modifier.get("value"); if (key == null) { query = query.equalTo(value); } else { query = query.equalTo(value, key); } } else if ("object".equals(valueType)) { if (key == null) { query = query.equalTo(null); } else { query = query.equalTo(null, key); } } }
What about React Native Expo, I'm having the same issue and I'm using React Native Expo and firebase library.
@mikehardy we are unable to do .on('value') for anything .equalTo(null) - it initially fetches but doesn't do any updates after that. A lot has changed since this thread was closed, any advice on how to listen to null?
@luskin No idea - if you can demonstrate it with a self-contained App.js (including data fixture creation for the test, then the test) that we can drop into a clean repo, we can look at it. It's possible that it is a limitation of firebase-android-sdk / firebase-ios-sdk or just firestore in general though (perhaps trying the same App.js in firebase-js-sdk is actually possible and easier as well?) - I'm not familiar with this issue unfortunately and it is quite old here