firebase 云函数迭代集合
firebase cloud functions iterate over collection
我正在建立一个论坛,当有人对某个主题发表评论时,我想通过电子邮件向所有人发送有人添加了评论。这需要遍历 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是什么,但是你可以这样做:
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 link,了解更多详情。
此外,如果有任何问题,我建议您通过 this link 正确设置 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
});
你需要运行获取查询,不知道这里的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 link,了解更多详情。
此外,如果有任何问题,我建议您通过 this link 正确设置 firestore 的云功能。