firebase云函数迭代集合

mjp*_*o23 0 node.js firebase google-cloud-functions google-cloud-firestore

我正在建立一个论坛,当有人对某个话题发表评论时,我想向每个人发送电子邮件,告知有人添加了评论。这需要迭代 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
 });
Run Code Online (Sandbox Code Playgroud)

Apo*_*ara 5

您需要运行 get 查询,不确定这里的 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());
});
Run Code Online (Sandbox Code Playgroud)

您可以查看链接,了解更多详细信息。

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