简体   繁体   English

如何在 netlify lamda 中使用 firebase 云函数的 firestore.onWrite()

[英]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.

相关问题 如何使用 firebase 云函数对 Firestore 数据进行逻辑处理 - How to use firebase cloud functions for logic on firestore data 与 firebase 云函数中的 firebase firestore 交互 - Interacting with firebase firestore from firebase cloud functions Firebase 云存储安全规则中Firestore文档参考如何使用? - How to use Firestore Document Reference in Firebase Cloud Storage Security Rules? 访问 doc.id in.onWrite firebase 云 function - accessing doc.id in .onWrite firebase cloud function 如何将 TS 路径映射与 Firebase 云函数一起使用 - How to use TS Path Mapping with Firebase Cloud Functions 如何在 firebase 云函数上正确使用 express-session - How to properly use express-session on firebase cloud functions Firestore - 获取路径未知的 ID 文档 - Firebase Cloud Functions - Firestore - Get document with ID whose path is unknown - Firebase Cloud Functions 数据未通过调度程序 function 和 Firebase Cloud Functions 保存到 Firestore - Data not saved into Firestore through scheduler function with Firebase Cloud Functions 如何在 Flutter web 应用程序中使用云 Firestore - How to use cloud firestore in Flutter web app 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM