I have a firestore database where I'm adding some documents with timestamp
fields like so:
async function addNewAppointment(appointment) {
appointment.appointmentDate = new Date(appointment.appointmentDate)
await setDoc(doc(db, 'appointments', appointment.appointmentID), appointment)
}
Here's how it looks like in firestore:
...
appointmentDate: February 1, 2023 at 11:00:00 AM UTC+5:30
...
Edit: Added images
When querying it like so:
async function getAllAppointments(options = { date: {} }) {
const constraints = []
if('from' in options.date) constraints.push(where('appointmentDate', '>=', new Date(options.date.from)))
if('to' in options.date) constraints.push(where('appointmentDate', '<', new Date(options.date.to)))
return (await getDocs(query(collection(db, 'appointments'), ...constraints))).docs.map(appointment => appointment.data())
}
I'm getting:
index.esm2017.js:5837 Uncaught (in promise) RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at Ba (index.esm2017.js:5837:88)
at index.esm2017.js:18652:27
at _m (index.esm2017.js:18686:15)
at Sm (index.esm2017.js:18573:5)
at index.esm2017.js:18982:13
at Um._parse (index.esm2017.js:18983:63)
at Um._apply (index.esm2017.js:18962:42)
at Bm (index.esm2017.js:18952:5)
When looking through the expected value for toISOString
, I saw that the ... at ...
in the middle of the timestamp is causing this issue.
I even checked and this is working:
> new Date('February 1, 2023 at 11:00:00 AM UTC+5:30').toISOString()
> VM15172:1 Uncaught RangeError: Invalid time value
at Date.toISOString (<anonymous>)
at <anonymous>:1:54
(anonymous) @ VM15172:1
> new Date('February 1, 2023 11:00:00 AM UTC+5:30').toISOString()
< '2023-02-01T05:30:00.000Z'
What do I do? How do I query properly?
Any help is appreciated!
I've looked at the only two results for this error under [google-cloud-firestore]
tag:
- RangeError: Invalid time value': No answer.
- rangeerror (index): invalid value: valid value range is empty: 0 in flutter while getting data from firebase: Not related to my question.
I found this question:
- Firestore query by date range: Not using modular sdk.