閱讀的文件:https://firebase.google.com/docs/functions/database-events
可以寫這些事件的處理常式
可以用參數,例如 ref(‘foo/{bar}’) ,那麼存取 foo/hello 或 foo/firebase 時,都會觸發常式對 foo/ 寫入,像下面
範例
onWrite 的時候,是用 change, context change 是 Change,提供了 before/after property 可以存取寫之前/之後的值 。
 
可以寫這些事件的處理常式
- onWrite() 資料被建立、更新或刪除時,會觸發
- onCreate() 資料被建立時,會觸發
- onUpdate() 資料被更新時,會觸發
- onDelete() 資料被刪除時,會觸發
可以用參數,例如 ref(‘foo/{bar}’) ,那麼存取 foo/hello 或 foo/firebase 時,都會觸發常式對 foo/ 寫入,像下面
{
  "foo": {
    "hello": "world",
    "firebase": "functions"
  }
}
會觸發兩次常式,一次是 foo/hello ,一次是 foo/firebase從 EventContext.params 可以用 bar 這個 key 去拿到 hello 或 firebase範例
 
// 當有訊息加到 /messages/:pushId/original 時,執行這常式。 
// 這常式會把指定路徑下的值,轉為大寫,再放到 /messages/:pushId/uppercase 
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original') 
    .onCreate((snapshot, context) => {  // snapshot 是 DataSnapshot ,context 是 EventContext 
      // 先取得寫入的值 
      const original = snapshot.val(); 
      console.log('Uppercasing', context.params.pushId, original); 
      const uppercase = original.toUpperCase(); 
      // 把資料寫到 "uppercase" sibling 然後回傳 Promise 
      // snapshot.ref.parent -> /messages/:pushId 
      // snapshot.ref.parent.child('uppercase') -> /messages/:pushId/uppercase 
      return snapshot.ref.parent.child('uppercase').set(uppercase); 
    }); 
用 EventContext 的 auth / authType 就可以取得 authentication 資訊 (這邊我有點迷糊了,為啥 authType 可以是 ADMIN / USER ?? onWrite 的時候,是用 change, context change 是 Change,提供了 before/after property 可以存取寫之前/之後的值 。
沒有留言:
張貼留言