2017-08-07 6 views
0

データを削除する際に、アプリのバッジを削除する必要があります(現在、サイレントプッシュ通知を使用してアプリのバッジを削除し、クラウド機能でアプリバッジ番号を減らす必要があります)。削除されました。しかし、削除したユーザーは別の場所にいる別のユーザーに複数のリクエストを送信する可能性があるため、Firebaseデータベースのトリガー機能で呼び出される関数を作成する必要があり、同じコードをどこにでもコピーしないようにする必要があります。クラウド関数の他の関数で呼び出すことができる関数を作るには?

機能は、このよう

function adminRemoveAppBadge(userID, dataID, categoryID) { 

}; 

そして、例えばおおよそになります

また
module.exports = functions.database.ref('/cards/{cardID}/interestedUsers/{interestedUserID}').onWrite(event => { 

    const currentData = event.data.current; 
    const prevData = event.data.previous; 

    const cardID = event.params.cardID; 
    const interestedUserID = event.params.interestedUserID; 

    if (currentData.val() && !prevData.val()) { 
     // value created 
     return console.log('cardInterestedUserHandler - created'); 
    } else if (!currentData.val() && prevData.val()) { 
     // value removed 
     console.log('cardInterestedUserHandler - removed', currentData.val()); 

     const cardRef = admin.database().ref("cards").child(cardID); 
     const cardRefPromise = cardRef.once("value", function(snap, error) { 
      if (error) { 
       return error; 
      }; 
      if (snap.val()) { 
       const cardJSON = snap.val(); 
       const cardOwnerID = cardJSON["ownerID"]; 

       if (cardOwnerID) { 
        const cardOwnerAppBadgesRef = admin.database().ref("userAppBadges").child(cardOwnerID).child("appBadgeModels").orderByChild("dataID").equalTo(cardID); 
        const cardOwnerAppBadgesRefPromise = cardOwnerAppBadgesRef.once("value", function (cardOwnerAppBadgesRefSnap, error) { 
         if (error) { 
          return error; 
         }; 
         if (cardOwnerAppBadgesRefSnap.val()) { 
          var deletingPromises = []; 
          cardOwnerAppBadgesRefSnap.forEach(function(cardOwnerAppBadgesRefSnapChild) { 
           const appBadgeModelJSON = cardOwnerAppBadgesRefSnapChild.val(); 
           const appBadgeModelID = appBadgeModelJSON["id"]; 
           const senderID = appBadgeModelJSON["senderID"]; 
           if (appBadgeModelID && senderID) { 
            if (senderID == interestedUserID) { 
             const cardOwnerAppBadgeRef = admin.database().ref("userAppBadges").child(cardOwnerID).child("appBadgeModels").child(cardOwnerAppBadgeModelID); 
             const cardOwnerAppBadgeRefPromise = cardOwnerAppBadgeRef.remove(); 
             deletingPromises.push(cardOwnerAppBadgeRefPromise); 


             // to call 
             adminRemoveAppBadge 
            }; 
           } else { 
            console.log("cardOwnerAppBadgeModelID == null"); 
           }; 
          }); 

          return Promise.all(deletingPromises); 
         }; 
        }); 
        return Promise.all([cardOwnerAppBadgesRefPromise]); 
       } else { 
        return console.log("owner id == null"); 
       }; 
      }; 
     }); 
     return Promise.all([cardRefPromise]); 
    } else { 
     return console.log('cardInterestedUserHandler - updated'); 
    }; 
}); 

の機能は別のファイルにあるこの機能でそれを呼び出します。他のfirebaseクラウド機能ではどのように呼び出すことができますか?この機能をどのように配備しますか?

更新私はそうherehereを書かれたオプションのいずれかを実行しようとしたが、私はデプロイを行うことを試みたとき、私は、エラーがモジュールのAppBadges/adminRemoveAppBadge.js「を見つけることができませんです。この機能を要求し

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

exports.adminRemoveAppBadge = function (userID, dataID, categoryID) { 

    console.log("adminRemoveAppBadge nil"); 
}; 

ので

var adminRemoveAppBadgeModule = require("AppBadges/adminRemoveAppBadge.js"); 

と、この関数を呼び出すので、

adminRemoveAppBadgeModule.adminRemoveAppBadge(cardOwnerID, cardID, 0); 

答えて

2

Googleの機能は、ちょうどJSある - ので、通常のルートは、コードの作業を含めること。

私は、だから私の機能のフォルダは次のようになりますフォルダ/lib に私の「ライブラリ」機能を配置します。私のindex.js

/functions 
    /lib 
     BuildImage.js 
     SendEmail.js 
index.js 
package.json 
...etc... 

私はちょうど私のコードが含まれています

const SendMail = require('./lib/SendMail') 
const sendMail = new SendMail({ 
    database: db, 
    mailgun: mailgun 
}) 
exports.sendContactUsMessage = functions.database.ref('/contact-messages/{itemId}').onWrite(sendMail.send(event)) 

EDITは/lib/SendMail.jsを追加しましたコード:

module.exports = class SendMail { 
constructor(config) { 
    if (!config) { 
    throw new Error ('config is empty. Must pass database and mailgun settings') 
    } 
    if (!config.database) { 
    throw new Error('config.database is empty. Must pass config.database') 
    } 
    if (!config.mailgun) { 
    throw 'config.mailgun is empty. Must pass config.mailgun' 
    } 
    this.database = config.database 
    this.mailgun = config.mailgun 
} 

sendEmail (emailData) { 
    return new Promise((resolve, reject) => { 
    this.mailgun.messages().send(emailData, (error, body) => { 
     if (error) { 
     if (debug) { 
      console.log(error) 
     } 
     reject(error) 
     } else { 
     if (debug) { 
      console.log(body) 
     } 
     resolve(body) 
     } 
    }) 
    }) 
} 
... 
} 
+0

あなたのSendMailは – Alexander

+0

のように見えます@Alexsander - 私のSendMail.jsファイルから関連するコードで答えを編集しました。 – JoeManFoo

+0

ありがとう! – Alexander

関連する問題