Jim
Jim

Reputation: 2312

Why is Firestore rejecting the operation?

I have a firestore action in a react-native project:

export const getMessages = (convoId) => {
    return (dispatch) => {
        firebase
            .firestore()
            .collection('messages')
            .where('userConvos', 'array-contains', convoId)
            .orderBy('time')
            .onSnapshot((querySnapshot) => {
                const messages = [];
                querySnapshot.forEach((doc) => {
                    messages.push(doc.data());
                });
                dispatch({ type: types.LOAD_MSGS, payload: messages });
            });
    };
};

the two composite indices I've tried in firestore:

enter image description here

and my database structure is like so:

The problem is when I make this firestore call, I get the

Error: Firestore: Operation was rejected because the system is not in a state required for the opereation's execution

But if the error is dismissed, the data is ordered as expected. What's going on and how can this error be eliminated?

Upvotes: 0

Views: 567

Answers (1)

george mano
george mano

Reputation: 6168

You have a missing index. To create it, do the following.

View the android device logs by typing on the terminal:

adb logcat 

If you reproduce the error, then something like the following will appear in the logs:

05-16 04:23:20.887 3746 3895 I ReactNativeJS: nativeErrorMessage: 'FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/XXXXXXXXX/firestore/indexes?create_composite=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',

Open a browser and paste the link.

Press the Create Index button.

Upvotes: 2

Related Questions