添加 Firebase 数据、点和正斜杠 [英] Adding Firebase data, dots and forward slashes

查看:14
本文介绍了添加 Firebase 数据、点和正斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 firebase db,我发现了非常重要的限制,firebase 帮助或常见问题解答中没有描述这些限制.

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,

即firebase 拒绝(原因不明) 下一个:

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}

在 firebase 中,您可以看到:

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...

顺便说一下'.''/' - firebase 的所有受限符号?

By the way '.' '/' - all restricted symbols for firebase ?

推荐答案

在 Firebase 中添加子02/10/2013 创建结构的原因是因为正斜杠导致创建一个新的高度.

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.

所以我假设您使用的是类似于以下内容的行:firebaseRef.child('02/10/2013').set(true) 等效于 firebaseRef.child('02').child('10').child('2013').set(true).

So the line I assume you are using something similar to: firebaseRef.child('02/10/2013').set(true) is equivalent to firebaseRef.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),

  • .(句号)
  • $(美元符号)
  • [(左方括号)
  • ](右方括号)
  • #(井号或井号)
  • /(正斜杠)

我们可以使用 JavaScript 的内置编码函数之一,因为据我所知,Firebase 没有提供内置方法来这样做.下面是一个运行,看看哪个对我们的目的最有效:

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:

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"

显然,最有效的解决方案是encodeURIComponent.但是,它并不能解决所有我们的问题.. 字符仍然存在问题,如上面的测试所示,并尝试 encodeURIComponent 您的测试电子邮件地址.我的建议是在 encodeURIComponent 之后链接一个替换函数来处理句点.

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 to encodeURIComponent your test e-mail address. My suggestion would be to chain a replace function after the encodeURIComponent to deal with the periods.

以下是您的两个示例案例的解决方案:

Here's what the solution would look like for your two example cases:

encodeURIComponent('Henry.Morgan@caribbean.sea').replace(/./g, '%2E') //results in "Henry%2EMorgan%40caribbean%2Esea"
encodeURIComponent('02/10/2013'); //results in "02%2F10%2F2013"

由于两个最终结果都可以安全地作为键名插入到 Firebase 中,唯一的另一个问题是从 Firebase 读取后解码,这可以用 replace('%2E', '.') 解决 和一个简单的 decodeURIComponent(...).

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 simple decodeURIComponent(...).

这篇关于添加 Firebase 数据、点和正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆