Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase retrieving doc ref, but not by equivalent query. What am I doing wrong?

If I create a document reference, and fetch it with getDoc, I get the document back just fine, but if I do a query for id == 'adsadasfsge' or id in ['adsadasfsge'] on the same database, I get nothing back. Here’s the code I’m trying:

    // Directly fetching one doc by id, using a docRef
    const dr = doc(firestore, 'TestPrograms', id);
    getDoc(dr).then((doc) => {
      console.log(doc.exists());
    });

    // Fetching with an unambiguous == query
    const q = query(collection(firestore, 'TestPrograms'), where('id', '==', id));
    getDocs(q).then((docs) => {
      console.log(docs.size);
    });
    // Fetching with an 'in' query
    const q2 = query(collection(firestore, 'TestPrograms'), where('id', 'in', [id]));
    getDocs(q2).then((docs) => {
      console.log(docs.size);
    });

Running this logs:

true
0
0

I’m baffled. What am I doing wrong here? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your query checks for a field named id inside the document with a specific value. If you want to check for documents whose document ID has a specific value, you need to specify the special documentId() marker as the field for the query.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading