![](/img/trans.png)
[英]How to use firebase cloud functions for logic on firestore data
[英]How to use firebase cloud functions' firestore.onWrite() in netlify lamda
我想聚合 Firestore 数据,但我想 go 和 netlify lambda 用于无服务器功能。 我想做类似的事情
onst functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.aggregateComments = functions.firestore
.document('posts/{postId}/comments/{commentId}')
.onWrite(event => {
const commentId = event.params.commentId;
const postId = event.params.postId;
// ref to the parent document
const docRef = admin.firestore().collection('posts').doc(postId)
// get all comments and aggregate
return docRef.collection('comments').orderBy('createdAt', 'desc')
.get()
.then(querySnapshot => {
// get the total comment count
const commentCount = querySnapshot.size
const recentComments = []
// add data from the 5 most recent comments to the array
querySnapshot.forEach(doc => {
recentComments.push( doc.data() )
});
recentComments.splice(5)
// record last comment timestamp
const lastActivity = recentComments[0].createdAt
// data to update on the document
const data = { commentCount, recentComments, lastActivity }
// run update
return docRef.update(data)
})
.catch(err => console.log(err) )
});
但我无法让它在 netlify lambda 上工作。 无论如何我可以在netlify lambda中使用这个function吗?
您不能将 Firebase Cloud Functions 部署到任何其他提供商。 其他环境可能完全不同,并且可能没有所需的所有凭据/环境变量。
如果您想在 GCP 之外侦听实时更新,可以尝试使用 Firestore 的onSnapshot()
,但您需要一个始终运行的服务器。 一旦您的无服务器函数终止,侦听器将停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.