「Gcp/Firebase/CloudFunctions/Realtimedatabaseトリガー」の版間の差分
(→件数取得サンプル) |
(→削除後データ取得) |
||
| (同じ利用者による、間の8版が非表示) | |||
| 行47: | 行47: | ||
}); | }); | ||
</pre> | </pre> | ||
| − | "/ChatLobbyUsers" | + | "/ChatLobbyUsers"の下のオブジェクト件数を、カウントして、"/UserCount"に件数を出力 |
| − | + | ===件数取得サンプルのパターン2=== | |
| + | <pre> | ||
| + | exports.countUsers = functions.database.ref('/Users/{roomKey}').onWrite((change, context) => { | ||
| + | if (!change.after.exists()) { | ||
| + | const count = 0; | ||
| + | if (change.before.exists()) { | ||
| + | return change.before.ref.parent.parent.child('/Rooms/' + context.params.roomKey + '/playerCount').set(count); | ||
| + | } | ||
| + | return null; | ||
| + | } | ||
| + | console.log("roomKey=" + context.params.roomKey); | ||
| + | const data = change.after.val(); | ||
| + | const count = Object.keys(data).length; | ||
| + | console.log("count=" + count); | ||
| + | return change.after.ref.parent.parent.child('/Rooms/' + context.params.roomKey + '/playerCount').set(count); | ||
| + | }); | ||
| + | </pre> | ||
| + | "/Users/2"の下のオブジェクト件数を、カウントして、"/Room/2/playerCount"に件数を出力 | ||
====参考==== | ====参考==== | ||
https://www.sukerou.com/2019/07/firebase-database.html | https://www.sukerou.com/2019/07/firebase-database.html | ||
https://polidog.jp/2018/08/07/firebase_counter/ | https://polidog.jp/2018/08/07/firebase_counter/ | ||
| + | |||
| + | ===削除サンプル=== | ||
| + | <pre> | ||
| + | exports.delUsers = functions.database.ref('/Users/{roomKey}/{userKey}').onDelete((snapshot, context) => { | ||
| + | const user = snapshot.val(); | ||
| + | if (user != null) { | ||
| + | console.log("onDelete user=" + user); | ||
| + | console.log("onDelete user.name=" + user.name); | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | /Users/hogeroom/user1を削除すると処理される。 | ||
| + | |||
| + | ===削除後データ取得=== | ||
| + | |||
| + | 参考:https://firebase.google.com/docs/database/admin/retrieve-data?hl=ja#node.js | ||
| + | のscoresRef.orderByValue().on部分参考。 | ||
==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合=== | ==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合=== | ||
| 行61: | 行95: | ||
参考:https://github.com/firebase/firebase-functions/issues/447 | 参考:https://github.com/firebase/firebase-functions/issues/447 | ||
| + | |||
| + | ==参考== | ||
| + | https://qiita.com/isyumi_net/items/7c67bb13371932056b9d | ||
| + | |||
| + | https://firebase.google.com/docs/database/admin/retrieve-data?hl=ja#node.js | ||
2021年10月11日 (月) 15:48時点における最新版
目次
functionsでrealtimedatabaseのトリガーを作る
公式:https://firebase.google.com/docs/functions/database-events?hl=ja
新規追加サンプル
exports.makeUppercase = functions.database.ref('/Rooms/{pushId}/original')
.onCreate((snapshot, context) => {
const original = snapshot.val();
functions.logger.log('Uppercasing', context.params.pushId, original);
const uppercase = original;
return snapshot.ref.parent.child('uppercase').set(uppercase);
例:"/Rooms/111/original"を、追加すると"/Rooms/111/uppercase"が、追加される。
追加更新削除サンプル
exports.makeUppercase = functions.database.ref('/Rooms/{pushId}/original2')
.onWrite((change, context) => {
if (change.before.exists()) {
return null;
}
if (!change.after.exists()) {
return null;
}
const original = change.after.val();
console.log('Uppercasing', context.params.pushId, original);
const uppercase = original;
return change.after.ref.parent.child('uppercase').set(uppercase);
});
例:"/Rooms/111/original2"を、追加すると"/Rooms/111/uppercase"が、追加される。
件数取得サンプル
exports.countUsers = functions.database.ref('/ChatLobbyUsers').onWrite((change, context) => {
if (!change.after.exists()) {
const count = 0;
if (change.before.exists()) {
return change.before.ref.parent.child('/UserCount').set(count);
}
return null;
}
const data = change.after.val();
const count = Object.keys(data).length;
console.log("count=" + count);
return change.after.ref.parent.child('/UserCount').set(count);
});
"/ChatLobbyUsers"の下のオブジェクト件数を、カウントして、"/UserCount"に件数を出力
件数取得サンプルのパターン2
exports.countUsers = functions.database.ref('/Users/{roomKey}').onWrite((change, context) => {
if (!change.after.exists()) {
const count = 0;
if (change.before.exists()) {
return change.before.ref.parent.parent.child('/Rooms/' + context.params.roomKey + '/playerCount').set(count);
}
return null;
}
console.log("roomKey=" + context.params.roomKey);
const data = change.after.val();
const count = Object.keys(data).length;
console.log("count=" + count);
return change.after.ref.parent.parent.child('/Rooms/' + context.params.roomKey + '/playerCount').set(count);
});
"/Users/2"の下のオブジェクト件数を、カウントして、"/Room/2/playerCount"に件数を出力
参考
https://www.sukerou.com/2019/07/firebase-database.html
https://polidog.jp/2018/08/07/firebase_counter/
削除サンプル
exports.delUsers = functions.database.ref('/Users/{roomKey}/{userKey}').onDelete((snapshot, context) => {
const user = snapshot.val();
if (user != null) {
console.log("onDelete user=" + user);
console.log("onDelete user.name=" + user.name);
}
}
/Users/hogeroom/user1を削除すると処理される。
削除後データ取得
参考:https://firebase.google.com/docs/database/admin/retrieve-data?hl=ja#node.js のscoresRef.orderByValue().on部分参考。
"TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合
cd functions npm install firebase-functions@latest vi functions/package.json
firebase-functionsを3.13.1以上にする。
参考:https://github.com/firebase/firebase-functions/issues/447
参考
https://qiita.com/isyumi_net/items/7c67bb13371932056b9d
https://firebase.google.com/docs/database/admin/retrieve-data?hl=ja#node.js
