Reputation: 1945
Since I can't use !=
, I''m using <
and >
but I don't know how to merge them together and await the result. It works for only one at a time.
async getAllUsersExceptCurrent() {
const lower = firebase
.firestore()
.collection('users')
.where(
firebase.firestore.FieldPath.documentId(),
'<',
firebase.auth().currentUser.uid
)
.get();
const upper = firebase
.firestore()
.collection('users')
.where(
firebase.firestore.FieldPath.documentId(),
'>',
firebase.auth().currentUser.uid
)
.get();
const usersList = await lower.then(snapshot => {
return snapshot.docs.map(user => {
console.log(user.data());
return { id: user.id, username: user.data().username };
});
});
return usersList;
}
Upvotes: 0
Views: 382
Reputation: 83093
The following should do the trick:
async getAllUsersExceptCurrent() {
const lower = firebase
.firestore()
.collection('users')
.where(
firebase.firestore.FieldPath.documentId(),
'<',
firebase.auth().currentUser.uid
)
.get();
const upper = firebase
.firestore()
.collection('users')
.where(
firebase.firestore.FieldPath.documentId(),
'>',
firebase.auth().currentUser.uid
)
.get();
const [l, u] = await Promise.all([lower, upper]);
const lArray = l.docs.map(user => {
return { id: user.id, username: user.data().username };
});
const uArray = u.docs.map(user => {
return { id: user.id, username: user.data().username };
});
return lArray.concat(uArray);
}
The following would also work:
//...
const results = await Promise.all([lower, upper, lower]);
const usersList = [];
results.forEach(r => {
r.docs.map(user => {
usersList.push({ id: user.id, username: user.data().username });
});
});
return usersList;
Upvotes: 1