gpt4 book ai didi

javascript - Cloud Functions 中的类型错误 : undefined is not a function - Promise. all() 错误

转载 作者:行者123 更新时间:2023-11-30 13:44:20 25 4
gpt4 key购买 nike

我正在尝试获取数据库中所有用户的文档 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 时遇到此错误:

//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()接受一系列 promise 。 usersRef 既不是数组,也不是 promise 。由于您已经等待 get() 返回的 promise ,因此 usersRef 成为 QuerySnapshot立即可用的对象,因此您需要按照这些条款使用它。由于它是快照不是引用,您可能应该以不同的方式命名。例如:

const usersSnapshot = await admin
.firestore()
.collection('Users')
.get();

const usersDocs = usersSnapshot.docs
console.log(usersDocs)

usersSnapshot.forEach(doc => {
console.log(doc)
})

关于javascript - Cloud Functions 中的类型错误 : undefined is not a function - Promise. all() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com