「Gcp/Firebase/CloudFunctions/Realtimedatabaseトリガー」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→"TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合) |
(→functionsでrealtimedatabaseのトリガーを作る) |
||
行2: | 行2: | ||
公式:https://firebase.google.com/docs/functions/database-events?hl=ja | 公式:https://firebase.google.com/docs/functions/database-events?hl=ja | ||
+ | サンプル | ||
+ | <pre> | ||
+ | exports.makeUppercase = functions.database.ref('/Rooms/{pushId}/original') | ||
+ | .onCreate((snapshot, context) => { | ||
+ | // Grab the current value of what was written to the Realtime Database. | ||
+ | const original = snapshot.val(); | ||
+ | functions.logger.log('Uppercasing', context.params.pushId, original); | ||
+ | // const uppercase = original.toUpperCase(); | ||
+ | const uppercase = original; | ||
+ | // You must return a Promise when performing asynchronous tasks inside a Functions such as | ||
+ | // writing to the Firebase Realtime Database. | ||
+ | // Setting an "uppercase" sibling in the Realtime Database returns a Promise. | ||
+ | return snapshot.ref.parent.child('uppercase').set(uppercase); | ||
+ | </pre> | ||
==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合=== | ==="TypeError: Cannot read property 'name' of undefined at RefBuilder.changeConstructor"エラーが出る場合=== | ||
cd functions | cd functions |
2021年10月8日 (金) 05:02時点における版
functionsでrealtimedatabaseのトリガーを作る
公式:https://firebase.google.com/docs/functions/database-events?hl=ja
サンプル
exports.makeUppercase = functions.database.ref('/Rooms/{pushId}/original') .onCreate((snapshot, context) => { // Grab the current value of what was written to the Realtime Database. const original = snapshot.val(); functions.logger.log('Uppercasing', context.params.pushId, original); // const uppercase = original.toUpperCase(); const uppercase = original; // You must return a Promise when performing asynchronous tasks inside a Functions such as // writing to the Firebase Realtime Database. // Setting an "uppercase" sibling in the Realtime Database returns a Promise. return snapshot.ref.parent.child('uppercase').set(uppercase);
"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