'String is not writing as a timestamp to firebase
so I had the date string Friday, April 15, 2022 and I converted it to Thu Apr 14 2022 00:00:00 GMT+0100 (Irish Standard Time) by string manipulation and now I'm writing it to firebase but it is saving as a string?
I am writing it to firebase like this:
const event = {
Time: eventTime,
start: newEventString,
title: eventName,
allDay: true
}
allEvents.push(event)
const db = fire.firestore();
db.collection("userEventsCal/"+currentUserUID+"/activities").add({event})
Because I am using reactFullCalendar, and the events isn't showing up in the calendar because of the date I think.
I am calling the user event info like below but when I use toDate() on the event start date it can't use it on a string because I know firebase returns a timestamp so I am not sure how to go about fixing this because none of the events are showing up in the react full calendar.
const getUserInfo2 = async () => {
let currentUserUID = fire.auth().currentUser.uid
console.log(currentUserUID)
const qSnap = await fire
.firestore()
.collection('userEventsCal')
.doc(currentUserUID)
.collection("activities")
.get()
const data = []
data = (qSnap.docs.map(d => ({
id: d.id,
title: d.data().event.title,
start: d.data().event.start.toDate(),
allDay: d.data().event.allDay,...d.data() })));
setData([...data])
}
Solution 1:[1]
You must save a Date or Firestore Timestamp object to save it as a timestamp in Firestore and not a string with a specific format:
const eventTime = "" // your date string
// TODO: convert the string to a Date
// const fireTimestamp = firebase.firestore.Timestamp.fromDate(new Date());
const eventTimeDate = new Date(eventTime)
// save this eventTimeDate in Firestore document
You might have to format your date string a bit to use Date() constructor and create a Date object. Checkout Parsing a string to a date in JavaScript for additional info.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharmaraj |
