0

I am creating a search bar where you can search for other users using firebase. I am currently struggling with querying through all my documents in my usernames collection, and checking if the users text input starts with what the user inputted. So for example, if 'Jessi' is searched, I would want to retrieve all users that start with those letters, such as 'Jessica' and 'Jessie'. I have tried many different solutions but am unable to complete the process.

This is what I have at the moment:

const searchUser = () => {
    const usersRef =
    firestore()
        .collection('usernames')
            .where('userName', '>=', username)
                .where('userName', '<=', username= + '~').get()
    console.log('usersRef', usersRef)
}

This however, returns an error: 'username' is read only.

These are my security rules:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read;
      allow write: if request.auth != null;
    }
  }
}

Any help would be greatly appreciated. I am really struggling here.

Junaid Razaq
  • 241
  • 5
  • 14
  • Firestore does not offer a flexible substring query. You can effectively query by string prefix using the strategy in the duplicate. – Doug Stevenson Jan 18 '21 at 06:14
  • What I need is to check if a document in my collection starts with what the user has inputted. Does the duplicate have the answer for this? Sorry I am a little confused. Thank you – Junaid Razaq Jan 18 '21 at 06:26
  • What you just said sounds different than what's in your question. I suggest editing the question to be more clear about what you're working with if it's different than the duplicate. If your situation is different, there should be enough information in the question to understand that. Ideally there is enough info that anyone can repro the same thing. – Doug Stevenson Jan 18 '21 at 06:48
  • Apologies. Hopefully this makes more sense as to what I am trying to accomplish. – Junaid Razaq Jan 18 '21 at 06:54
  • It's still a string prefix search covered by the duplicate. – Doug Stevenson Jan 18 '21 at 07:25
  • After trying the solution in the duplicate, unfortunately I am receiving the error 'username is read only' – Junaid Razaq Jan 18 '21 at 07:27

0 Answers0