michael powers
michael powers

Reputation: 207

Can the Date in a firestore query (onsnapshot) be kept current

In my web app I fire off a query to gather all recent messages for a user and then use an onsnapshot to show these messages in a feed. In my app some messages get posted to the future so I only want messages from now or earlier in the feed.

  var query = firebase.firestore()
                  .collection('messages')
                  .where('owner','==',userID)
                  .where('timestamp','<',new Date())
                  .orderBy('timestamp', 'desc')
                  .limit(25);

The user can then create new messages and I want those added to the feed displayed in the app. After adding a new message do I have to cancel the onSnapshot listener and execute a new query with an updated current Date? Or is there a way for this query to update the time lookup so that the onSnapshot is always getting newly posted messages?

Upvotes: 3

Views: 682

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317903

If you want to change any of the query parameters, you need to build a new Query object with the new values and add a snapshot listener to it..

Upvotes: 1

Related Questions