I have 'users' collection which contains documents of each user ID. I've also stored the current user's ID in a userID variable using the authenticated user's ID (anonymously signed in).
I need to get one document ID (not the document data) based on a counter of the lowest value (document data item within this collection), and store it in a variable called 'receiverID', but if the document ID equals the current user's ID, then I would like to get the next available ID.
For example, if there are 3 users on my platform (A, B, and C) and the following 'counter' values exist:
- A: 1, B: 2, C: 3
theoretically, when A queries the collection, they will get their own doc ID back. I would then need to get the second document available within the 'users' collection.
I've begun with the following but am stuck:
receiverID = db.collection('users').orderBy('Questions Received','asc').limit(1)
Also, this is triggered on an event listener (when a user submits a question), so the 'receiverID' must be resolved before the next part of the function. This is what my event listener looks like:
const sayForm = document.querySelector('#say-form');
sayForm.addEventListener('submit', (e) => {
e.preventDefault();
db.collection('message').add({
message: sayForm.sayInput.value,
time: firebase.firestore.FieldValue.serverTimestamp(),
sender: userID,
currentReceiver: receiverID,
received: 1
})
.catch(function(error) {
console.error("Error adding document: ", error);
});
});