【问题标题】:firebase cloud functions iterate over collectionfirebase 云函数迭代集合
【发布时间】:2022-01-03 08:56:38
【问题描述】:

我正在建立一个论坛,当有人在某个线程上遇到问题时,我想通过电子邮件向所有人发送有人添加了评论。这需要遍历 firestore 集合。如何使用 firebase 云功能做到这一点?

exports.onCommentCreation = functions.firestore.document('/forum/threads/threads/{threadId}/comments/{commentId}')
 .onCreate(async(snapshot, context) => {

     var commentDataSnap = snapshot; 

     var threadId = context.params.threadId; 
     var commentId = context.params.commentId; 

     // call to admin.firestore().collection does not exist
     var comments = await admin.firestore().collection('/forum/threads/threads/{threadId}/comments/');

     // iterate over collection
 });

【问题讨论】:

  • “调用 admin.firestore().collection 不存在”是什么意思???你如何导入adminfirebase.google.com/docs/firestore/query-data/… 展示了如何遍历集合
  • 您是否在收藏查询中尝试过`/forum/threads/threads/${threadId}/comments/`
  • 答案有效,收集电话有效

标签: node.js firebase google-cloud-firestore google-cloud-functions


【解决方案1】:

你需要运行get query,不确定这里的admin是什么,但你可以这样做:

const citiesRef = db.collection('cities'); // pass your collection name
const snapshot = await citiesRef.where('capital', '==', true).get(); // query collection
if (snapshot.empty) {
  console.log('No matching documents.');
  return;
}  

snapshot.forEach(doc => {
  console.log(doc.id, '=>', doc.data());
});

您可以查看this链接,了解更多详情。

另外,如果有任何问题,我建议您通过this link正确设置firestore的云功能。

【讨论】:

  • 好的,谢谢,这行得通。我能够迭代收集快照
猜你喜欢
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2011-04-25
  • 1970-01-01
  • 2019-01-18
  • 2019-06-24
相关资源
最近更新 更多