簡體   English   中英

如何一次運行2個Firebase雲功能

[英]How to run 2 firebase cloud functions at a time

我想一次嘗試一些方法來運行2個Firebase雲函數,但是在觸發函數時拋出錯誤。 我在數據庫中有2個孩子,當孩子中發生任何更改時,它應該向用戶顯示通知。 誰能幫我這個忙。

這是數據庫圖像,顯示數據庫中有2個孩子:

點擊查看圖片

這是我的index.js文件

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

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

const fooModule = require('./foo');
const barModule = require('./bar');

exports.foo = functions.database.ref('app_title').onWrite(fooModule.handler);
exports.bar = functions.database.ref('db_kinda/{Uid}/comment').onWrite(barModule.handler);

這是我的foo.js文件

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

// Listens for new debate topic created
exports.appTitle = (event) => {
    console.log('Push notification event triggered');

       const app_title = change.after.val();

        if (!change.after.exists() && change.before.exists()) {
            return change.before.val();         
        }

        const payload = {notification: {
            title: 'New Notification',
            body: `${app_title}`
        }};

   return admin.messaging().sendToTopic("appGlobal",payload)
       .then(function(response){
            console.log('Notification sent successfully:',response);
       })
       .catch(function(error){
            console.log('Notification sent failed:',error);
       });
};

這是我的bar.js文件

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

exports.data = (event) => {
    console.log('Push notification event triggered');

       const comment = change.after.val();

        if (!change.after.exists() && change.before.exists()) {
            return change.before.val();         
        }

        const payload = {notification: {
            title: 'New Notification',
            body: `${comment}`
        }};

   return admin.messaging().sendToTopic("appGlobal",payload)
       .then(function(response){
            console.log('Notification sent successfully:',response);
       })
       .catch(function(error){
            console.log('Notification sent failed:',error);
       });
};

此功能已正確部署,但在Firebase功能上運行時會引發如下錯誤

TypeError:處理程序不是cloudFunction(/srv/node_modules/firebase-functions/lib/cloud-functions.js:127:23)在/worker/worker.js:825:24處的函數._tickDomainCallback(internal /進程/next_tick.js:229:7)

如錯誤所示, handler不是函數。 例如,當您導入foo ,您將獲得具有名為appTitle而不是handler的屬性的exports值。 嘗試這個:

const fooModule = require('./foo');
exports.foo = functions.database.ref('app_title').onWrite(fooModule.appTitle);

或者,將您的./foo導出更改為:

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

// Listens for new debate topic created
exports.handler = (event) => {
  ...
};

暫無
暫無

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

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