0

I'm trying to update a document in my firestore from my React app. To update the value, I'm trying to get the document where the value uid is equal to my current user's id. I'm using Firebase V9 and here is my code :

        if(myDescription.length < 15){
            alert("The description must be longer !")
            return
        }
        
        const currentUID = auth.currentUser.uid

        const myDoc = doc(collection(db, "descriptions"), where("uid", "==", currentUID))
        await setDoc(myDoc, {desc: myDescription})

But when I execute it I have got this error :

Uncaught (in promise) TypeError: n.indexOf is not a function
    at rt.fromString (index.esm2017.js:1032:1)
    at sa (index.esm2017.js:16044:1)
    at handleClick (Description.js:38:1)
    at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1)
    at invokeGuardedCallback (react-dom.development.js:4277:1)
    at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4291:1)
    at executeDispatch (react-dom.development.js:9041:1)
    at processDispatchQueueItemsInOrder (react-dom.development.js:9073:1)
    at processDispatchQueue (react-dom.development.js:9086:1)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
raffgiga21
  • 57
  • 1
  • 6

1 Answers1

1
doc(collection(db, "descriptions"), where("uid", "==", currentUID))

The doc function doesn't expect query conditions like where. It's expecting the second argument to be a string, so when it tries to interact with it as a string, it throws an exception.

Instead, you need to use query:

const q = query(collection(db, "descriptions"), where("uid", "==", currentUID))
const snapshot = await getDocs(q);

The snapshot can include an arbitrary number of documents, so you may need to add code to handle what to do if 0 or more than 1 are returned. But you might do something like this:

const firstDoc = snapshot.docs[0];
if (firstDoc) {
  await setDoc(firstDoc.ref, {desc: myDescription})
} else {
  console.error("uh oh, it's not there")
}
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98