好的方法来替换Firebase中的无效字符? [英] Good way to replace invalid characters in firebase keys?

查看:120
本文介绍了好的方法来替换Firebase中的无效字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是保存用户的信息。当我尝试使用用户的电子邮件地址作为密钥将数据保存到Firebase时,Firebase将引发以下错误:
$ b


错误:无效的密钥e @ e.ee(不能包含。$ []#

,显然,我不能通过他们的电子邮件索引用户信息。什么是最好的做法来取代



我已经成功更改改为 - 但是不会因为某些电子邮件有 - 在地址中。



目前,我正在使用

  var cleanEmail = email。取代( '` ''。'); 

但是有可能会与此冲突。在电子邮件地址中,用逗号替换点



逗号不是允许的字符,电子邮件地址,但 在Firebase密钥中是允许的。对称地,点 在电子邮件地址中是允许的字符,但在Firebase密钥中不允许 。所以直接替代会解决你的问题。您可以索引电子邮件地址,而无需循环。



您还有其他问题。在你的代码中:

  var cleanEmail = email.replace('。',','); 

只会替换第一个点但电子邮件地址可以有多个点。要替换所有点实例,应该使用正则表达式。如下:

  var cleanEmail = email.replace(/\./g,','); 


My use case is saving a user's info. When I try to save data to Firebase using the user's email address as a key, Firebase throws the following error:

Error: Invalid key e@e.ee (cannot contain .$[]#)

So, apparently, I cannot index user info by their email. What is the best practice to replace the .?

I've had success changing the . to a - but that won't cut it since some email's have -s in the address.

Currently, I'm using

var cleanEmail = email.replace('.','`');

but there are likely going to be conflicts down the line with this.

解决方案

In the email address, replace the dot . with a comma ,. This pattern is best practice.

The comma , is not an allowable character in email addresses but it is allowable in a Firebase key. Symmetrically, the dot . is an allowable character in email addresses but it is not allowable in a Firebase key. So direct substitution will solve your problem. You can index email addresses without having to loop over anything.

You also have another issue. In your code:

var cleanEmail = email.replace('.',',');

will only replace the first dot . But email addresses can have multiple dots. To replace all dot instances, you should use a regular expression instead. As follows:

var cleanEmail = email.replace(/\./g, ',');

这篇关于好的方法来替换Firebase中的无效字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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