I try to use firebase db, I found very important restrictions, which are not described in firebase help or FAQ.
First problem is that symbol: dot '.' prohibited in keys,
i.e. firebase reject (with unknown reason) next:
nameRef.child('Henry.Morgan@caribbean.sea').set('Pirat');
Second problem with forward slashes in your keys '/', when you try to add key like this
{'02/10/2013': true}
In firebase you can see:
'02': {
'10': {
'2013': true
}
}
Have you got any ideas how to solve it (automatically)? May be set some flag that it is string key with all symbols? Of course, I can parse/restore data every time before write and after read, but...
By the way '.' '/' - all restricted symbols for firebase ?
I faced the same problem myself, and I have created firebase-encode for this purpose.
Unlike the chosen answer, firebase-encode encodes only unsafe characters (./[]#$) and % (necessary due to how encoding/decoding works). It leaves other special characters that are safe to be used as firebase key while
encodeURIComponent
will encode them.Here's the source code for details:
If you're using Swift 3, this works for me (try it in a playground):
I got annoyed with this problem so I took the answer from @sushain97 (thanks!) and built a deep encoder/decoder.
https://www.npmjs.com/package/firebase-key-encode
Basic usage:
Deep Usage:
I don't see an "automatic" built in FireBase encoder for keys.
Here is a Java solution.
I built this, a simplified version of josue.0's answer, but I think it is better code since his version could cause problems. A lot of people will use _P or _D in their code, so it needs to be more complex and unlikely.
The reason that adding a child
02/10/2013
creates a structure in Firebase is because the forward slashes are resulting in the creation of a new level.So the line I assume you are using something similar to:
firebaseRef.child('02/10/2013').set(true)
is equivalent tofirebaseRef.child('02').child('10').child('2013').set(true)
.To avoid the problems of not being able to use the following characters in reference key names (source),
we can use one of JavaScript's built in encoding functions since as far as I can tell, Firebase does not provide a built in method to do so. Here's a run-through to see which is the most effective for our purposes:
Evidently, the most effective solution is
encodeURIComponent
. However, it doesn't solve all our problems. The.
character still poses a problem as shown by the above test and trying toencodeURIComponent
your test e-mail address. My suggestion would be to chain a replace function after theencodeURIComponent
to deal with the periods.Here's what the solution would look like for your two example cases:
Since both the final results are safe for insertion into a Firebase as a key name, the only other concern is decoding after reading from a Firebase which can be solved with
replace('%2E', '.')
and a simpledecodeURIComponent(...)
.I wrote this for java (since I came here expecting a java implementation):