如何在netlify lamda中使用firebase cloud functions' firestore.onWrite()



我想聚合firestore数据,但我想为无服务器函数使用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中使用这个函数?

您不能将Firebase云函数部署到任何其他提供程序。其他环境可能完全不同,并且可能没有所需的所有凭据/env变量。

如果你想监听GCP之外的实时更新,你可以尝试使用Firestore的onSnapshot(),但你需要一个始终运行的服务器。一旦无服务器函数终止,侦听器就会停止。

相关内容

最新更新