好的方法来替换Firebase中的无效字符? [英] Good way to replace invalid characters in firebase keys?
问题描述
$ 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屋!