swr-firestore: Bug with useCollection and where by timeStamp.

Love using this lib so far. I am trying to make a paginated list of messages for a messaging app, and I think I stumbled on a bug. Because of the JSON serialization / maybe something else in useCollection, where: ['time', '<', new Data(Date.now())] doesn’t return anything, but a regular get on a collection ref does.

const collectionPath = `path/to/collection`
const orderBy = ['time', 'desc']

const currentDate = new Date(Date.now())

fuego.db
  .collection(collectionPath)
  .orderBy('time', 'desc')
  .where('time', '<', currentDate)
  .get()
  .then((data) => console.log(data.empty)) // data.empty === false

const { data } = useCollection(
  collectionPath,
  {
    orderBy: ['time', 'desc'],
    where: ['time', '<', currentDate],
    listen: true,
  },
)

// data always is empty array.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 27 (10 by maintainers)

Most upvoted comments

Hello, any workarounds that I can query the timestamp by range at this moment?

That’s because the date gets recreated on each render, so it would fetch every time. You probably want this:

const date = useRef(new Date(…)).current

and put that inside your component.

but yeah, currently this only works with a milliseconds time stamp as this issue states.

~The date is created outside the component, surely it would not be re-created every render in that case unless you use lazy loading components (which I believe still wouldn’t suffer much from this problem anyway?~

Scratch that, missed reading the strike throughed part that moving it outside would solve it. Using a ref would solve it if you need a dynamic date, or a memo in some cases.

That’s because the date gets recreated on each render, so it would fetch every time. You probably want this:

const date = useRef(new Date(…)).current

and put that inside your component.

but yeah, currently this only works with a milliseconds time stamp as this issue states.