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

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

问题描述

我尝试使用 firebase db,
我发现了非常重要的限制,这些限制都没有描述第一个问题是符号:点'。'禁止在键,



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

  nameRef.child('Henry.Morgan@caribbean .SEA ')设置为(' Pirat'); 

当您尝试添加/,
时,这样的键

  {'02 / 10/2013':true} 

在firebase中,您可以看到:

$ p $ '02' :{
'10':{
'2013':true
}
}

你有什么想法如何解决(自动)?
可能会设置一些标志,它是所有符号的字符串键?
当然,我可以在每次读写之前解析/恢复数据,但是...


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

解决方案

添加子元素 02/10/2013 code>在Firebase中创建一个结构是因为正斜杠导致创建一个新的级别。因此,我假设你使用类似的东西: firebaseRef.child('02 / 10/2013')。set(true) 相当于 firebaseRef.child('02')。child('10')。child('2013').set(true)



为了避免在引用键名称中使用下列字符的问题(资源),



  • 。 (期间)
    $(美元符号)
  • [(左方括号)

  • ](右方括号)
  • #(散列或英镑符号)
  • /(正斜杠)

我们可以使用JavaScript内置的编码函数之一,因为据我所知,Firebase没有提供内置的这样做的方法。下面是一个看看哪个是最有效的我们的目的:
$ b $ pre $ var forbiddenChars ='。$ []# ; //包含禁止的字符
escape(forbiddenChars); //结果为。%24%5B%5D%23 /
encodeURI(forbiddenChars); // result in。%24%5B%5D%23%2F
encodeURIComponent(forbiddenChars); //结果是。%24%5B%5D%23%2F

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



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

  encodeURIComponent('Henry.Morgan@caribbean.sea')。replace(/\./g,'%2E')//结果是Henry%2EMorgan%40caribbean%2Esea
encodeURIComponent('02 /二千零十三分之一十' ); //结果为02%2F10%2F2013​​

由于最终结果对于插入一个Firebase作为一个关键的名字,唯一的另外一个问题就是从Firebase中读取数据,然后用替换('%2E','。')和一个简单的 decodeURIComponent(...)


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 ?

解决方案

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 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),

  • . (period)
  • $ (dollar sign)
  • [ (left square bracket)
  • ] (right square bracket)
  • # (hash or pound sign)
  • / (forward slash)

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"

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"

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天全站免登陆