react-native-firebase icon indicating copy to clipboard operation
react-native-firebase copied to clipboard

firebase database equalTo(null) doesn't work as expected on Android

Open chungweileong94 opened this issue 7 years ago • 8 comments

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.

chungweileong94 avatar Mar 13 '19 04:03 chungweileong94

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?

silvafacundo avatar Mar 27 '19 00:03 silvafacundo

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);
      }
    }
  }

Wojew avatar Apr 07 '19 18:04 Wojew

You should propose a PR @Wojew - if it works well others might need it

mikehardy avatar Apr 07 '19 18:04 mikehardy

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.

stale[bot] avatar May 05 '19 19:05 stale[bot]

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.

stale[bot] avatar May 20 '19 19:05 stale[bot]

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.

dev-neeraj avatar Dec 05 '19 07:12 dev-neeraj

@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 avatar Jan 19 '21 23:01 luskin

@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

mikehardy avatar Jan 20 '21 00:01 mikehardy