Adding Firebase data, dots and forward slashes

后端 未结 9 2054
长情又很酷
长情又很酷 2020-12-01 12:32

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

相关标签:
9条回答
  • 2020-12-01 13:21

    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.

    public static String encodeForFirebaseKey (String s) {
        s = s.replace(".", "_P%ë5nN*")
                .replace("$", "_D%5nNë*")
                .replace("#", "_H%ë5Nn*")
                .replace("[", "_Oë5n%N*")
                .replace("]", "_5nN*C%ë")
                .replace("/", "*_S%ë5nN")
        ;
        return s;
    }
    
    public static String decodeFromFirebaseKey(String s) {
    
        s = s.replace("_P%ë5nN*", ".")
                .replace("_D%5nNë*", "$")
                .replace("_H%ë5Nn*", "#")
                .replace("_Oë5n%N*", "[")
                .replace("_5nN*C%ë", "]")
                .replace("*_S%ë5nN", "/");
    
        return s; 
    
    0 讨论(0)
  • 2020-12-01 13:22

    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:

    var firebaseKeyEncode = require('firebase-key-encode');
    firebaseKeyEncode.encode('my.bad.key');
    // Output: my%2Ebad%2Ekey
    

    Deep Usage:

    var firebaseKeyEncode = require('firebase-key-encode');
    
    var badTree = {
        "pets": [
            {
                "jimmy.choo": 15}
            ],
        "other.key": 5
    }
    
    firebaseKeyEncode.deepEncode(badTree);
    
    // Output: {
    //    "pets": [
    //        {
    //            "jimmy%2Echoo": 15}
    //        ],
    //    "other%2Ekey": 5
    // }
    
    0 讨论(0)
  • 2020-12-01 13:23

    Personally, I found a simple and easy hack for this same problem I encountered

    I took the dateTime string and convert it using replace('/','|')

    the result will be something like this 2017|07|24 02:39:37 instead of 2017/07/24 02:39:37.

    0 讨论(0)
提交回复
热议问题