2017-05-10 8 views
0

Firebaseのクラウド機能を使用してデータベース用の関数を作成しようとしています。この関数の目的は、attendテーブルにイベントを書き込むのを聞き、イベントを識別するために書き込まれたオブジェクトに基づいて、イベントオブジェクトのusersAttendingをインクリメントします。Firebaseのクラウド機能を使用したインクリメンタル操作

これまでの私の機能です。

//listens to write on attendObjects (when a user is attending an event), and increments attending users for event 
exports.listenAttendingEvents = functions.database.ref('/attend/{pushId}').onWrite(event => { 

//get attendObj -> parsed JSON by javascript interpreter 
const attentObj = event.data.val(); 
const attendId = attentObj['attendId']; 
const pathToAttendees = '/attends' + '/' + attendId; 

// Attach an asynchronous callback to read the data at our posts reference 
admin.database().ref(pathToAttendees).on("value", function(snapshot) { 
    console.log(snapshot.val()); 
    const obj = snapshot.val(); 
    var nrAttending = obj['attending']; 
     nrAttending = Number(snapshot.val()); 
     return admin.database().ref(pathToAttendees + '/attending').transaction(function (nrAttending) { 
      return (nrAttending || 0) + 1; 
     }); 
    }); 
}, function (errorObject) { 
    console.log("The read failed: " + errorObject.code); 
    return errorObject 
}); 

event objectが取得されないという問題があります。関数はそれ以前の状態で終了するようです。ok

+2

を解決しました。詳細については、こちらのページを参照してください。https://firebase.google.com/docs/functions/terminate-functions –

+0

@FrankvanPuffelen ...ありがとうございました。 – John

答えて

0

問題は、私のトップレベル関数の約束がないということでした。これにより、操作が完了する前にGoogle Cloud機能によって強制終了されました。約束を追加

はYou'eが非同期データベーストランザクションがまだ進行中である間、Googleクラウド機能は、それを殺すことを意味し、あなたのトップレベルの機能から、約束を返さない私の問題

admin.database().ref(pathToAttendees).once("value").then(function(snapshot) { 
関連する問題