Search code examples
firebasefirebase-realtime-databasefirebase-security

Firebase Real Time Database Validate Number Increment


I'm using Firebase Real-Time Database as backend. I want it to increase by 1 max for each request For example:

    "Counter":{
       ".write":"true",
       ".read":"true,
       ".validate":"data.val()+1"
    }

This is not working.

On the frontend, I'm sending "data+1". How should i create ".validate" rules?


Solution

  • To validate that the new value is one higher than the existing value:

    ".validate":"newData.val() === data.val() + 1"
    

    Also see the Firebase documentation on Existing Data vs. New Data.