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

标签 javascript firebase google-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 时遇到此错误:

//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/

相关文章:

android - 删除 Firebase Android 应用程序,然后在另一个项目中使用其 SHA1 和包名称

python - 在 Python 中写入流式传输到 Google Cloud Storage

javascript - 单击 div 后在 Javascript 中移动表格

javascript - 使用 CSS/Javascript/JQuery 分隔表格单元格

firebase - 如何防止Firestore中不必要的文档读取?

node.js - 从 firebase 返回响应的正确方法是 Google Cloud Pub/Sub 触发器

android - Firebase 计划为 strip 集成付款

typescript - 从 onCreate 的 Firestore snap.data 检索数据但出现未定义的错误

javascript - 如何在不在服务器上创建页面的情况下将字符串解释为 HTML 页面

javascript - jQuery 输入名称参数