0

i have a probleme when i try to add a new document to my firebase database

button.addEventListener('click', e => {
    e.preventDefault();
    let task = input.value;
    addDoc(colRef, {
      userId: 1,
      task: task,
      completed: false
    }).then(() => {
      input.value = "";
    })
})


onSnapshot(colRef,(snapshot) => {
  snapshot.docs.map((doc) => {
      todos.push(doc.data())
  
  })
  console.log(todos)

my todos array instead of having 11 todos it had 21 enter image description here

i tried this but it didn't work

onSnapshot(colRef,(snapshot) => {
  snapshot.docs.map((doc) => {
    if(!(doc.data() in todos)){
      todos.push(doc.data())
    }
  })
  console.log(todos)`
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    check out wrapping it in a useEffect hook with an unsubscribe function. you can find some details here: "https://stackoverflow.com/questions/59944658/which-react-hook-to-use-with-firestore-onsnapshot" – Michael Martell Feb 16 '23 at 23:12

0 Answers0