[英]How to use firebase cloud functions' firestore.onWrite() in netlify lamda
I want to aggregate firestore data but I want to go with netlify lambda for the serverless functions.我想聚合 Firestore 数据,但我想 go 和 netlify lambda 用于无服务器功能。 I want to do something like
我想做类似的事情
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) )
});
but I can't get it to work on netlify lambda.但我无法让它在 netlify lambda 上工作。 Is there anyway I can use this function in netlify lambda?
无论如何我可以在netlify lambda中使用这个function吗?
You cannot deploy Firebase Cloud Functions to any other provider.您不能将 Firebase Cloud Functions 部署到任何其他提供商。 Other environments might be totally different and may not have all the credentials/env variables required.
其他环境可能完全不同,并且可能没有所需的所有凭据/环境变量。
If you want to listen for realtime updates outside of GCP, you can try using Firestore's onSnapshot()
but you'll need a server that runs always.如果您想在 GCP 之外侦听实时更新,可以尝试使用 Firestore 的
onSnapshot()
,但您需要一个始终运行的服务器。 The listener will stop once you serverless functions terminates.一旦您的无服务器函数终止,侦听器将停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.