ipmcc
ipmcc

Reputation: 29916

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!

Upvotes: 0

Views: 29

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599081

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.

Upvotes: 1

Related Questions