The following code works great to retrieve all the items in the collection. However, the uids are not available here and I need them to be able to compare with other userRefs.
app.get("/api/all-users", (req, res) => {
let arr = [], i = 0;
db.collection('users').get()
.then((result) => {
result.forEach((doc) => {
arr[i] = doc.data();
i++
})
res.json(arr);
})
.catch((err) => {
res.json(err);
})
})
This returns a json response:
users: [
{name: 'Name', username: 'Username' etc ..}
]
However I was looking for something with the actual uids. How would I get that information with a query like this?
Assuming you mean the document ID where you say uid (which is typically used to denote a user ID).
Since you only return doc.data()
you are dropping the document ID. If you want to return both, you could do:
app.get("/api/all-users", (req, res) => {
let arr = [], i = 0;
db.collection('users').get()
.then((result) => {
result.forEach((doc) => {
arr[i] = { ...doc.data(), id: doc.id };
i++
})
res.json(arr);
})
.catch((err) => {
res.json(err);
})
})
Or a simpler version of the same:
db.collection('users').get()
.then((result) => {
res.json(result.docs.map(doc => {
return { ...doc.data(), id: doc.id }
});
})