(Firebase Firestore)TypeError: n.indexOf is not a function

Viewed 887

I'm trying to add another field value in a document but firebase returns TypeError: n.indexOf is not a function. Here's the code:

async function linkLCSN(cor, sn) {
  try {
    await setDoc(doc(db, "cor", cor), {
      sn: sn,
    }, {merge: true});
  } catch(e) {
    console.error(e);
  }
} 

I've already succeeded in doing this way but I don't know why this time it keeps giving me this error. This is the working code:

async function submitToDatabase(name, email, cor, cs, cn, concern) {
    try {
        //Set Datas 
        await setDoc(doc(db, "cor", cor), {
        name: name,
        email: email,
        cor: cor,
        courseSection: cs,
        contactNumber: cn,
        isViewed: false,
        timestamp: serverTimestamp(),
        }, {merge: true});
        const docRef = await addDoc(collection(db, "cor", cor, "concerns"), {
        concernData: concern,
        });
        console.log("Yung betlog nasa:" + docRef.id);
        //Do page changes
        let a = document.querySelector(".concern-main-container");
        let b = document.querySelector(".concern-preview-container");
        a.style.display = "none";
        b.style.display = "block";
    } catch(e) {
        console.error(e);
        //Custom Alert
    }
}
2 Answers

I solved my problem by checking every type of params that passing to doc(), that params should not be Integer. It must be String

I'm assuming you are using v9 of firebase (modular version). It may sound silly, but make sure that you have initialized your app and that you are using a valid reference to a collection.

For example:

import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';

const config = {/** your firebase config properties **/};

const app = initializeApp(config);
const db = getFirestore(app);

Then to add a new document, you can do:

const createDocument = (collectionName, document) => {
    const colRef = collection(db, collectionName);
    return addDoc(colRef, document);
};

Remember the addDoc function returns a promise, so be sure to handle this is the caller of createDocument

Related