0

React Native Firebase to access Firestore database

Database The field selectedJobTypesArr is an array of numbers. The elements are numbers because they come direct from a picklist

database snippet

Operation . I have a function that uses array-contains-any to match any value in selectedJobTypesArr

export const hasMatchingJobType = (user_id, jobTypeCodeArr) => {
  return new Promise((resolve, reject) => {
    let foundFlag = false;
    firestore()
      .collection('profiles')
      .where('user_id', '==', user_id)
      .where('selectedJobTypesArr', 'array-contains-any', jobTypeCodeArr)
      .orderBy('preferredName')
      .get()
      .then((value) => {
        value.docs.map((doc) => {
          foundFlag = true;
        });
        resolve(foundFlag);
      });
  });
};

To test it I call it as follows:

    const user_id = 'TUJawBQN9ge9qNl1l5Qi320UZXK2';
    const jobTypeCodeArr = [101, 102, 103, 104, 105];

    hasMatchingJobType(user_id, jobTypeCodeArr).then((res) => {
      console.log('res ', res);
    });

Expected Behaviour returns true when called with matching data

Actual Behaviour Returns false on IOS. If the database array elements are numbers, the array-contains-any only returns expected results with Android not IOS. If I change the database elements to string, both android and IOS produce expected results.

Environment React Native Firebase to access Firestore database version @react-native-firebase/[email protected]

Android build.gradle:

 ext {
    buildToolsVersion = "29.0.2"
    minSdkVersion = 21
    compileSdkVersion = 30
    targetSdkVersion = 30
}

IOS: podfile containing directive $FirebaseSDKVersion = '8.0.0'

1 Answer 1

1

const jobTypeCodeArr = ['101', '102', '103', '104', '105'];

This seems to be an array of strings but your Firestore document has an array of numbers which are not same. You must pass an array of numbers in where() to get your documents.

const user_id = 'TUJawBQN9ge9qNl1l5Qi320UZXK2';
const jobTypeCodeArr = [101, 102, 103, 104, 105];

hasMatchingJobType(user_id, jobTypeCodeArr).then((res) => {
  console.log('res ', res);
});

If you want to use the existing arrays which has numbers as strings then you would have to change the data type to string in Firestore as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, pasted wrong version of test data. It is indeed numbers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.