IL5*_*L55 16 slash firebase firebase-realtime-database
我尝试使用firebase db,我发现了非常重要的限制,这些限制在firebase帮助或FAQ中没有描述.
第一个问题是符号:点'.' 禁止钥匙,
即firebase 拒绝(原因不明)下一个:
nameRef.child('Henry.Morgan@caribbean.sea').set('Pirat');
Run Code Online (Sandbox Code Playgroud)
当您尝试添加这样的键时,在键'/'中使用正斜杠的第二个问题
{'02/10/2013': true}
Run Code Online (Sandbox Code Playgroud)
在firebase中你可以看到:
'02': {
'10': {
'2013': true
}
}
Run Code Online (Sandbox Code Playgroud)
你有任何想法如何解决它(自动)?可以设置一些标志,它是所有符号的字符串键吗?当然,我可以在写入之前和读取之后每次解析/恢复数据,但是......
顺便说说 '.' '/' - firebase的所有限制符号?
sus*_*n97 24
添加子项02/10/2013在Firebase中创建结构的原因是正斜杠导致创建新级别.
所以我假设你使用类似于:firebaseRef.child('02/10/2013').set(true)相当于的行firebaseRef.child('02').child('10').child('2013').set(true).
为避免无法在引用键名称(源)中使用以下字符的问题,
- .(期)
- $(美元符号)
- [(左方括号)
- ](右方括号)
- #(哈希或井号)
- /(正斜杠)
我们可以使用JavaScript的内置编码功能之一,因为据我所知,Firebase 没有提供内置方法.这是一个贯穿始终,看看哪个对我们的目的最有效:
var forbiddenChars = '.$[]#/'; //contains the forbidden characters
escape(forbiddenChars); //results in ".%24%5B%5D%23/"
encodeURI(forbiddenChars); //results in ".%24%5B%5D%23%2F"
encodeURIComponent(forbiddenChars); //results in ".%24%5B%5D%23%2F"
Run Code Online (Sandbox Code Playgroud)
显然,最有效的解决方案是encodeURIComponent.但是,它并没有解决我们所有的问题.该.字符仍然存在问题,如上述测试所示,并尝试encodeURIComponent测试您的测试电子邮件地址.我的建议是在encodeURIComponent处理句点之后链接替换函数.
以下是两个示例案例的解决方案:
encodeURIComponent('Henry.Morgan@caribbean.sea').replace(/\./g, '%2E') //results in "Henry%2EMorgan%40caribbean%2Esea"
encodeURIComponent('02/10/2013'); //results in "02%2F10%2F2013"
Run Code Online (Sandbox Code Playgroud)
由于最终结果都可以安全地插入Firebase作为关键名称,因此唯一的另一个问题是从Firebase读取后解码,可以通过replace('%2E', '.')简单解决decodeURIComponent(...).
我自己也遇到了同样的问题,为此我创建了firebase-encode.
与所选答案不同,firebase-encode仅编码不安全字符(./ [] #$)和%(由于编码/解码如何工作所必需).它encodeURIComponent会保留其他特殊字符,这些字符可以安全地用作firebase密钥,同时对它们进行编码.
这是详细信息的源代码:
// http://stackoverflow.com/a/6969486/692528
const escapeRegExp = (str) => str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
const chars = '.$[]#/%'.split('');
const charCodes = chars.map((c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
const charToCode = {};
const codeToChar = {};
chars.forEach((c, i) => {
charToCode[c] = charCodes[i];
codeToChar[charCodes[i]] = c;
});
const charsRegex = new RegExp(`[${escapeRegExp(chars.join(''))}]`, 'g');
const charCodesRegex = new RegExp(charCodes.join('|'), 'g');
const encode = (str) => str.replace(charsRegex, (match) => charToCode[match]);
const decode = (str) => str.replace(charCodesRegex, (match) => codeToChar[match]);
Run Code Online (Sandbox Code Playgroud)
https://www.firebase.com/docs/creating-references.html中记录了字符限制- 您不能使用'.','/','[',']','#'和'$'用钥匙名.没有自动的方式来逃避这些角色,我建议完全避免使用它们或创建自己的转义/转义机制.
我为java写了这个(因为我来到这里期待一个java实现):
public static String encodeForFirebaseKey(String s) {
return s
.replace("_", "__")
.replace(".", "_P")
.replace("$", "_D")
.replace("#", "_H")
.replace("[", "_O")
.replace("]", "_C")
.replace("/", "_S")
;
}
public static String decodeFromFirebaseKey(String s) {
int i = 0;
int ni;
String res = "";
while ((ni = s.indexOf("_", i)) != -1) {
res += s.substring(i, ni);
if (ni + 1 < s.length()) {
char nc = s.charAt(ni + 1);
if (nc == '_') {
res += '_';
} else if (nc == 'P') {
res += '.';
} else if (nc == 'D') {
res += '$';
} else if (nc == 'H') {
res += '#';
} else if (nc == 'O') {
res += '[';
} else if (nc == 'C') {
res += ']';
} else if (nc == 'S') {
res += '/';
} else {
// this case is due to bad encoding
}
i = ni + 2;
} else {
// this case is due to bad encoding
break;
}
}
res += s.substring(i);
return res;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11750 次 |
| 最近记录: |