简体   繁体   English

无法为Cloud Firestore触发Firebase功能

[英]Firebase Functions not firing for Cloud Firestore

I simply cannot see where I'm going wrong here. 我根本看不到我要去哪里哪里。 My Cloud Firestore is on "europe-west3", the functions are deployed to "europe-west1" (the docs tell me that this is the closest location to west3). 我的Cloud Firestore位于“ europe-west3”上,功能已部署到“ europe-west1”(文档告诉我,这是距离west3最近的位置)。

Structure is thus: I've got a bunch of "tickets" each of which can have a subcollection named "comments". 因此,结构为:我有一堆“票”,每个“票”都可以有一个名为“ comments”的子集合。 The console thus looks like this: 因此,控制台如下所示:

安慰

The upload was successful: 上载成功:

功能控制台

The function code was taken from the official code samples Github repo for Function samples 该功能代码取自官方代码示例Github回购的功能示例

This is what my code looks like: 这是我的代码如下所示:

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

exports.countComments = functions.region('europe-west1').database.ref('/tickets/{ticketId}/comments/{commentsid}')
    .onWrite(
        async (change) => {
            const ticketsRef = change.after.ref.parent;
            const countRef = ticketsRef.parent.child('comments_count');

            let increment;
            if(change.after.exists() && !change.before.exists()) {
                increment = 1;
            } else if(!change.after.exists() && change.before.exists()) {
                increment = -1;
            } else {
                return null;
            }

            await countRef.transaction((current) => {
                return (current || 0) + increment;
            });
            console.log('Counter updated');
            return null;
        });

exports.recountComments = functions.region('europe-west1').database.ref('/tickets/{ticketId}/comments_count')
    .onDelete(
        async (snap) => {
            const counterRef = snap.ref;
            const collectionRef = counterRef.parent.child('comments');
            const commentsData = await collectionRef.once('value');
            return await counterRef.set(commentsData.numChildren());
        }
    )

Now, the problem is that these functions simply do not fire. 现在,问题在于这些功能根本无法启动。 I'm not seeing anything in the logs, regardless of whether I'm pushing changes through my clients (a Flutter app) or if I'm changing things directly in the Firebase console. 无论是通过客户端(Flutter应用程序)推送更改,还是直接在Firebase控制台中更改内容,我都不会在日志中看到任何内容。

In my desperation I've also tried to simply listen to "/tickets" as any changes below that path should also trigger - but there's nothing. 在绝望中,我还试图简单地听“ / tickets”,因为该路径下的任何更改也应触发-但是没有任何东西。

So. 所以。 What is the obvious thing I overlooked? 我忽略了哪些显而易见的事情? And, yes, I had a look at the other questions/answers but nothing jumped at me... 而且,是的,我查看了其他问题/答案,但是没有发现我...

edit: 编辑:

This would be the corrected version, probably not optimal. 这将是更正的版本,可能不是最佳的。

exports.countComments = functions.region('europe-west1').firestore.document('/tickets/{ticketId}/comments/{commentsId}')
    .onWrite(
        async (change, context) => {
            const ticketId = context.params.ticketId;
            const ticketRef = admin.firestore().collection('tickets').doc(ticketId);

            let increment;
            if(change.after.exists && !change.before.exists) {
                increment = 1;
            } else if(!change.after.exists && change.before.exists) {
                increment = -1;
            } else {
                return null;
            }
            return transaction = admin.firestore().runTransaction(t => {
                return t.get(ticketRef)
                    .then(doc => {
                        let count = (doc.data().comments_count || 0) + increment;
                        t.update(ticketRef, {comments_count: count});
                    });
            }).then(res => {
                console.log('Counter updated');
            }).catch(err => {
                console.log('Transaction error:', err);
            });
        });

Your database is Cloud Firestore, but you've written a Realtime Database trigger. 您的数据库是Cloud Firestore,但是您已经编写了一个实时数据库触发器。 They are two completely different databases. 他们是两个完全不同的数据库。 Follow the documentation for writing Cloud Firestore triggers instead. 请遵循文档来编写Cloud Firestore触发器

Your function will start like this: 您的函数将像这样开始:

functions.region('europe-west1').firestore.document('...')

Note "firestore" instead of "database". 注意“ firestore”,而不是“数据库”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM