Home > OS >  Writing to Firebase Firestore React
Writing to Firebase Firestore React

Time:02-22

so I have a problem right now. I'm entering the users dates into cloud firestore like this:

enter image description here

so this is a user collection, with a document by user's id's and then the dates are entered as a list. But whenever I refresh the page and enter new data, all the previous data disappears.

So I'm wondering how do I enter data so that it goes like collection(userCalendar).doc(USERID).collection(dates) and then it has all the user's data entered as strings rather than an array like I've been doing.

My code for the way it's behaving right now is below. Thank you! :)

export const allEvents = [];

const Calendar = () => {
const [date, setData] = useState([]);



const handleDateClick = async (DateClickArg) => {


      if (DateClickArg.jsEvent.altKey) {
        const title = prompt("Enter title", DateClickArg.dateStr); // allows user to put a title in
        // making object
        const event = {
          title: title ? title : DateClickArg.dateStr,
          start: DateClickArg.date,
          allDay: true
        }

        allEvents.push(event)

        const db = fire.firestore();
        let currentUserUID = fire.auth().currentUser.uid
        const doc = await fire
          .firestore()
          .collection("userCalendar")
          .doc(currentUserUID)
          .get()

        db.collection("userCalendar")
          .doc(currentUserUID)
          .set({
            activites: allEvents
          })

      }
}

CodePudding user response:

Not sure whether this meets your requirement, but from my understanding you just want to update the activities with the allEvents which contains all the updated activities.

    db.collection("userCalendar")
      .doc(currentUserUID)
      .set({
        activites: allEvents
      })

should become

    db.collection("userCalendar")
      .doc(currentUserUID)
      .set({
        activites: allEvents
      }, { merge: true })

Or you can use the update method

    db.collection("userCalendar")
      .doc(currentUserUID)
      .update({
        activites: allEvents
      })

From the docs

To update some fields of a document without overwriting the entire document, use the update() method:

import { doc, setDoc } from "firebase/firestore"; 

const cityRef = doc(db, 'cities', 'BJ');
setDoc(cityRef, { capital: true }, { merge: true });

CodePudding user response:

It looks like you're overwriting your collection with every code execution:

db.collection("userCalendar")
          .doc(currentUserUID)
          .set({
            activites: allEvents
          })

You should consider to make an array union, so that the values are added to your collection instead of overwriting them:

db.collection("userCalendar")
          .doc(currentUserUID)
          .update({
            activites: firebase.firestore.FieldValue.arrayUnion(
                            {
                                allEvents
                            }),
          })

Also some examples from firestore docu: https://cloud.google.com/firestore/docs/samples/firestore-data-set-array-operations

  • Related