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
を解決しました。詳細については、こちらのページを参照してください。https://firebase.google.com/docs/functions/terminate-functions –
@FrankvanPuffelen ...ありがとうございました。 – John