Firebase JS Uncaught (in promise) TypeError: doc is not a function

Viewed 88

I'm working on a web app using firebase and encountered this error. After a few hours of debugging, I still haven't been able to understand what's wrong, I'll leave the code here for anyone who can help me figure out what its is that I'm doing wrong. Any suggestion will be really helpful, thanks!

Code:

    import {
      getFirestore,
      addDoc,
      doc,
      updateDoc,
      collection,
      query,
      where,
      getDocs,
      setDoc,
    } from "firebase/firestore";

const app = initializeApp(firebaseConfig);
const firestore = getFirestore(app);

var files = [....]

getDocs(collection(firestore, "property")).then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
      if (files.includes(decrypt(doc.data().fileno))) {
        if (files.includes(decrypt(doc.data().fileno), 6)) {
          let owner = "ABC";
          const bref = doc(collection(firestore, "property"), doc.id);
          updateDoc(bref,{owner:encrypt(owner)}).then(()=>{
            console.log("updated")
          })

        } else {
          let owner = "XYZ";
          const bref = doc(collection(firestore, "property"), doc.id);
          updateDoc(bref,{owner:encrypt(owner)}).then(()=>{
            console.log("updated")
          })
        }
      }
    });
  });
1 Answers

You have two definitions of doc in your code:

  1. The doc function that you import from the Firestore SDK
  2. The doc parameter that you declare in querySnapshot.forEach((doc) => {.

The second doc hides the first one, which is why you can't call the doc function anymore inside that callback.

The solution is to give the variable a different name, like docSnapshot.

Related