Reputation: 852
I am trying to fetch the document ID's of all the users in my database. For which I have written this code:
exports.scheduledFunction = functions.pubsub
.schedule('every 2 minutes')
.onRun(async context => {
console.log('This will be run every 2 minutes!');
try {
const usersRef = await admin //Works Perfectly, I get the QuerySnapshot of the collection
.firestore()
.collection('Users')
.get();
console.log('usersRef: ', usersRef);
const userDocs = await Promise.all(usersRef); //This gives the error
console.log('User Docs: ', userDocs);
} catch (err) {
console.log('err: ', err);
}
return null;
});
I obtain this error on passing the QuerySnapshot promise in Promise.all()
:
//Error
TypeError: undefined is not a function
at Function.all (<anonymous>)
at exports.scheduledFunction.functions.pubsub.schedule.onRun (/srv/index.js:624:38)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
I expected to collect all the document IDs from the result of Promise.all()
Help would be very much appreciated.
Upvotes: 1
Views: 548
Reputation: 317828
Promise.all() takes an array of promises. usersRef
is neither an array, nor a promise. Since you're already awaited the promise returned by get(), that makes usersRef
a QuerySnapshot object that's immediately available, so you will need to work with it on those terms. Since it's a snapshot and not a reference, you should probably name it differently. For example:
const usersSnapshot = await admin
.firestore()
.collection('Users')
.get();
const usersDocs = usersSnapshot.docs
console.log(usersDocs)
usersSnapshot.forEach(doc => {
console.log(doc)
})
Upvotes: 3
Reputation: 600006
The await Promise.all
is not needed, since you already load all user documents int the first statement with get()
and await
.
So it should be:
const usersDocs = await admin
.firestore()
.collection('Users')
.get();
console.log('User Docs: ', userDocs);
Upvotes: 1