Skip to content Skip to sidebar Skip to footer

Firestore Query Where Field Is Not Equal To Value Using Indexes With OnSnapshot

I am trying to get all unseen messages that the current user has in conversation. The problem is that I do not know how to exclude other user seen messages and get only current use

Solution 1:

When I user firestore for the first time, these things was also a problem for me. Being very hard to do a simple count query.

So for your question, the simple answer is you can get the authenticated user id and add another where query something like

firebase.auth().onAuthStateChanged((user) => {
  if (user) {
    // User logged in already or has just logged in.
    console.log(user.uid);
  } else {
    // User not logged in or has just logged out.
  }
});
.where('userId', '==', userId)

But my suggestion is to use a firebase function to count the number of unseen messages using firestore triggers. So in your function you would do something like

const functions = require('firebase-functions');

exports.countUnseenMessages = functions.firestore
  .document('...')
  .onWrite((change, context) => {
    // increase number of unseen messages to the relevant user
  });

Hope that helps


Post a Comment for "Firestore Query Where Field Is Not Equal To Value Using Indexes With OnSnapshot"