Using the % operator on path keys throws and error

Viewed 34

In the documentation for the firabase realtime database it says for the % sign:

Used to find the remainder of dividing one variable by another in a rules expression.
This write rule grants write access to users with even-numbered user IDs:

"$user_id": {
 ".write": "$user_id % 2 === 0"
}

However when i try to use it, it throws an error saying that "left operand is not number".

They touch on this briefly in the docs:

Note: PATH KEYS ARE ALWAYS STRINGS. Because keys are always strings, attempting to compare a $ variable to a number will always fail. This can be corrected by converting the number to a string using the + operator.

However I think I must have misunderstood it, because I keep getting the same error.

What am I missing?

1 Answers

firebaser here

I tried myself and asked around, and it looks like there is no way to convert the key to a number here, which is what is required for the % to be possible. The only way to implement this scenario would be to (also) include the user_id as the value of a property in the JSON, and not just as a key.

Sorry about the documentation misfire. I've filed a bug for this, and will get it updated in the documentation shortly.

Related