Can I implement beforeCreate trigger on Firebase Functions

后端 未结 2 688
醉梦人生
醉梦人生 2021-01-19 01:18

On the Firebase docs they mention 4 types of triggers:

  • onCreate
  • onDelete
  • onUpdate
  • onWrite

Is there a way to listen t

2条回答
  •  攒了一身酷
    2021-01-19 01:34

    All events for the Realtime Database in Cloud Functions trigger asynchronously after the write has been committed. For this reason, other users may already have seen the data before your function can change it.

    To solve this problem you'll want to ensure the data only gets written to the location everyone sees after it's been validated/modified.

    To validate/modify the new data before listeners to that data can see it, you have two options:

    1. Use a HTTP triggered function for writing the data. The application code calls the HTTP function, which does the data manipulation you want, and then writes the result to the database.

    2. Have the applications write to a "moderation queue", which is just a separate location in the database. The Cloud Function triggers fro this queue, validates/modifies the data, writes it to the actual location, and then deletes it from the queue.

    With both of these approaches you lose parts of the transparent offline behavior of the Firebase Realtime Database though, so you'll have to choose.

提交回复
热议问题