The error happens in this code:
document.querySelector('.adding').onclick = async function adding() { const output = document.querySelector('.added') const translate = collection(db, "translate") await setDoc(translate, { eng_tot, sank_tot }); output.innerText = 'Added' console.log('added', english, sanskrit) english.value = '' sanskrit.value = '' eng_tot = [] sank_tot = [] setTimeout(() => { output.innerText = '' }, 2000); };
** I am trying from 2 days but can’t understand what’s the problem !!**
I am trying to add an array in firebase , i am tring so many solution but can’t get what’s the problem !!
Advertisement
Answer
The setDoc()
function takes a DocumentReference as first parameter but you are passing a CollectionReference. If you want to add a document with random ID then use addDoc()
instead:
import { collection, addDoc } from "firebase/firestore" const translate = collection(db, "translate") await addDoc(translate, { eng_tot, sank_tot });
If you want to specify the document ID yourself, then use doc()
to create a document reference and then use setDoc()
:
import { doc, setDoc } from "firebase/firestore" const translateDoc = doc(db, "translate", "custom_doc_id") await setDoc(translateDoc, { eng_tot, sank_tot });