skip to Main Content

I’m using "where" query to get a single document so that I can update it. I need the docID which is what i’m struggling with. I’m following the documentation but not much progress. I’m using Firebase cloud functions, Nodejs and Javascript.

const admin = require("firebase-admin");
const firestore = admin.firestore();

exports.bucketTesting = functions.https.onRequest(async (req, res) => {

//code to source file.metadata.mediaLink

  const imagesRef = firestore.collection("images");
  const specificImageRef = await imagesRef
    .where("imageUrl", "==", file.metadata.mediaLink)
    .get();
  console.log(specificImageRef);

  specificImageRef.forEach((doc) => {
    console.log(doc.id, "=>", doc.data());
  });
}

The console.log(specificImageRef) print data(that i can’t use) but the console.logs in the forEach do not print anything.

How do I get the docID?

2

Answers


  1. Your comment: "How do i get a document or the docID?"

    Your code is correct to get the document docID and data but as discussed in the comments the query returns 0 documents.

    The only adaptation you could do since you are sure that there is only one document corresponding to the query is to use the docs array as follows:

    exports.bucketTesting = functions.https.onRequest(async (req, res) => {
    
      const imagesRef = firestore.collection("images");
      const querySnapshot = await imagesRef
              .where("imageUrl", "==", file.metadata.mediaLink)
              .get();
    
      if (querySnapshot.size === 0) {
         // No document found
         // Send back a message or an error
         res.status(500).send("No document found");
      } else {
         const uniqueDocSnapshot = querySnapshot.docs[0];
         console.log(uniqueDocSnapshot.id, "=>", uniqueDocSnapshot.data());
    
         res.send(...);
      }
    
            
    
    
    })
    
    Login or Signup to reply.
  2. const admin = require("firebase-admin");
    const firestore = admin.firestore();
    
    exports.bucketTesting = functions.https.onRequest(async (req, res) => {
    
      //code to source file.metadata.mediaLink
    
      const imagesRef = firestore.collection("images");
      
      // the following will return an array 
      const specificImages = (await imagesRef
        .where("imageUrl", "==", file.metadata.mediaLink)
        .get()).docs.map(each => each.data()) || [];
        
        
      /* the console log on the next line will show the array
      it will show multiple items if there is more than one document
      where imageUrl === file.metadata.mediaLink */
      console.log(specificImages);
      
      /* all is left to do is to return the array 
        or return a single document */
        
      // the following is the case when you are just returning a single document
      res.send({
        desiredData: specificImages?.[0] ?? {} // fallback if there is no document
      })
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search