簡體   English   中英

無法為Cloud Firestore觸發Firebase功能

[英]Firebase Functions not firing for Cloud Firestore

我根本看不到我要去哪里哪里。 我的Cloud Firestore位於“ europe-west3”上,功能已部署到“ europe-west1”(文檔告訴我,這是距離west3最近的位置)。

因此,結構為:我有一堆“票”,每個“票”都可以有一個名為“ comments”的子集合。 因此,控制台如下所示:

安慰

上載成功:

功能控制台

該功能代碼取自官方代碼示例Github回購的功能示例

這是我的代碼如下所示:

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());
        }
    )

現在,問題在於這些功能根本無法啟動。 無論是通過客戶端(Flutter應用程序)推送更改,還是直接在Firebase控制台中更改內容,我都不會在日志中看到任何內容。

在絕望中,我還試圖簡單地聽“ / tickets”,因為該路徑下的任何更改也應觸發-但是沒有任何東西。

所以。 我忽略了哪些顯而易見的事情? 而且,是的,我查看了其他問題/答案,但是沒有發現我...

編輯:

這將是更正的版本,可能不是最佳的。

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);
            });
        });

您的數據庫是Cloud Firestore,但是您已經編寫了一個實時數據庫觸發器。 他們是兩個完全不同的數據庫。 請遵循文檔來編寫Cloud Firestore觸發器

您的函數將像這樣開始:

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

注意“ firestore”,而不是“數據庫”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM