facebook twitter hatena line email

「Gcp/Firebase/CloudFunctions/Realtimedatabaseトリガー」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(追加更新削除サンプル)
(件数取得サンプル)
行33: 行33:
 
===件数取得サンプル===
 
===件数取得サンプル===
 
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/
  
 
==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合===
 
==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合===

2021年10月8日 (金) 13:35時点における版

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"が、追加される。

件数取得サンプル

https://www.sukerou.com/2019/07/firebase-database.html

https://polidog.jp/2018/08/07/firebase_counter/

"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