向我的Firebase参考URL添加动态子级 [英] Adding a Dynamic Child to my Firebase Reference URL

查看:67
本文介绍了向我的Firebase参考URL添加动态子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直试图将一个孩子动态地添加到我的Firebase数据引用URL中.

I've been attempting to dynamically add a child to my firebase data reference URL, so far with no luck.

假设我具有以下数据结构:

Assume I have the following data structure:

MyApp
| -beta_signups
|-用户
| --fred
| ----通过电子邮件发送"fred@test.com"

MyApp
|-beta_signups
|-users
|--fred
|----email "fred@test.com"

我希望能够根据孩子在注册"部分下的电子邮件地址添加注册的孩子.这是我尝试过的方法,但是没有用.

I would like to be able to add people who sign up, as a child based on their email address under the "signups" section. here is what I tried, but it didn't work.

var myDataRef = new Firebase("https://myapp.firebaseio.com/beta_signups/");
  $('#submit').click(function() {
      var email = $('#email').val();
      myDataRef.child(email).push({email: email, beta_key: false});
      $('#email').val('We got it.');
  });

关于如何动态添加孩子的任何建议?

Any suggestions on how I can dynamically add the child?

推荐答案

您不能将电子邮件地址用作子路径的键,因为它包含无效字符.请参阅文档中的创建参考.

You can't use an email address as the key for a child path because it contains invalid characters. See Creating References in the docs.

您还将基于电子邮件地址创建子项,然后使用push创建电子邮件的子项.可能您应该只删除.child(email)位,并使用push创建记录.

You are also creating a child based on the email address, and then creating a child of the email by using push. Probably, you should just get rid of the .child(email) bit and use push to create the records.

var user_id = myDataRef.push({email: email, beta_key: false}).name();

首先要问的是您是否真的想通过电子邮件存储用户.通常,ID会有用得多(它们可能稍后会更改他们的电子邮件,在这种情况下,您必须重新输入系统中所有用户的数据).

The first thing to ask is whether you actually want to store the users by email. Generally, an ID is going to be much more useful (they may change their email later, in which case you have to go re-key all their user data in the system).

如果这是一个要求,那么您要么必须对它们进行哈希处理或对其进行清理.例如:

If that's a requirement, then you're either going to have to hash them or sanitize them. For example:

// replace all forbidden characters with something that won't appear in the email address
var key = email.replace(/[.$\[\]\/#]/, ','/);
myDataRef.child(key).set({email: email, beta_key: false});

这篇关于向我的Firebase参考URL添加动态子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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