iandev
iandev

Reputation: 191

Firebase v9 React JS IndexOf is not a function

for some reason it is throwing me an error in the console "TypeError: n.indexOf is not a function" and I do not understand very well why it happens. I am using Firestore and Firebase v9 Modular and basically it is a function that creates a category in firestore database and also inside the category creates a test task.

Here is the full error:

TypeError: n.indexOf is not a function
    at Function.fromString (index.esm2017.js:1032)
    at va (index.esm2017.js:14990)
    at App.jsx:48

And the line 48 in the error message is:

const taskRef = doc(collection(db, "categorias"), where("uid", "==", currentUser), newDocRef.id)

Here is my code :)

const currentUser = '1234'

const crearCategoria = useCallback(async() => {
    try {
      const newDocRef = doc(collection(db, "categorias"));

      await setDoc(newDocRef, {
        name: categoryName,
        uid: currentUser,
        projectId: newDocRef.id
      })

      const taskRef = doc(collection(db, "categorias"), where("uid", "==", currentUser), newDocRef.id)
      await setDoc(taskRef, {
       name: 'prueba',
       uid: currentUser,
       projectId: newDocRef.id
    })

    } catch (error) {
      console.log(error)
    }
  })

Upvotes: 6

Views: 7997

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

As Dharmaraj commented, you cannot perform an update on a query. You'll instead have to:

  1. Execute the query to find the matching document(s).
  2. Loop over the documents in your application code.
  3. Update each of them in turn.

In code that should be something like:

const taskQuery = doc(collection(db, "categorias"), where("uid", "==", currentUser))
const taskDocs = await getDocs(taskQuery)
taskDocs.forEach((taskDoc) => {
  await setDoc(taskDoc.ref, {
    name: 'prueba',
    uid: currentUser,
    projectId: newDocRef.id
  })
})

Upvotes: 4

Related Questions