![](/img/trans.png)
[英]Promise.all error inside async function : undefined is not a function
[英]Type error: undefined is not a function - Promise.all() error in Cloud Functions
我正在尝试获取数据库中所有用户的文档 ID。 我为此编写了这段代码:
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;
});
我在Promise.all()
传递 QuerySnapshot 承诺时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)
我希望从Promise.all()
的结果中收集所有文档 ID
非常感谢帮助。
Promise.all()接受一组承诺。 usersRef
既不是数组,也不是承诺。 由于您已经在等待 get() 返回的承诺,这使得usersRef
成为一个立即可用的QuerySnapshot对象,因此您需要根据这些条件使用它。 由于它是快照而不是参考,您可能应该对其进行不同的命名。 例如:
const usersSnapshot = await admin
.firestore()
.collection('Users')
.get();
const usersDocs = usersSnapshot.docs
console.log(usersDocs)
usersSnapshot.forEach(doc => {
console.log(doc)
})
不需要await Promise.all
,因为您已经使用get()
和await
将所有用户文档加载到第一个语句中。
所以应该是:
const usersDocs = await admin
.firestore()
.collection('Users')
.get();
console.log('User Docs: ', userDocs);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.