Firebase在创建帐户时创建额外的用户数据 [英] Firebase creating extra user data upon account creation

查看:137
本文介绍了Firebase在创建帐户时创建额外的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase和Angular 2来创建新用户。我想通过为每个新创建的用户添加额外的数据来扩展这一点。在这个例子中,我希望每个新创建的用户都以一个指定的属性净值= 5000 开始。所以我的模式看起来像这样:

  email | ''
密码|净值
'5000'

通过 Firebase文档,我发现保存数据功能。但是,我不确定如何将 set 功能与我的用户名和密码认证脚本进行合并。

以下是基本的注册脚本。

 注册(){
firebase.auth()。createUserWithEmailAndPassword('johncena@nosell.com','ucantcme ')
.catch(function(error){
console.log(error);
});}

我想知道如何用这个注册实现 set 特性,所以我可以为用户添加默认数据。

解决方案

您不能在auth区域保存'net_worth'。为此,你需要在数据库中有一个独立的表。例如用户。请检查以下代码。



创建用户后,您将获得 UID

  firebase.auth()。createUserWithEmailAndPassword('johncena@nosell.com' ('response'); //创建用户UID 

var updates = {};
updates ['/ users /'+ response.uid] = {net_worth:5000};
firebase.database().ref().update(updates);
});

这里'users'是一个新表。


I am using Firebase with Angular 2 to create new users. I would like to extend this by adding additional data for each newly created user. In this example, I would like to have every newly created user start with an assigned property of net worth = 5000. So my schema would look something like:

email | ''
password | ''
net worth | '5000'

Having gone through the Firebase docs, I have found the save data feature. However, I am unsure how to merge the set feature with my username and password authentication script.

Below is the basic signup script.

signup(){
     firebase.auth().createUserWithEmailAndPassword('johncena@nosell.com', 'ucantcme')
     .catch(function(error){
     console.log(error);
});}

I would like to know how to implement the set feature with this signup so I can add default data for users.

解决方案

You can't save 'net_worth' in the auth area. For that you need a seperate table in the database . For example 'users'. Check the below code.

After user creation you get UID. Using that UID store the related data into the database.

firebase.auth().createUserWithEmailAndPassword('johncena@nosell.com', 'ucantcme')
   .then(function(response) {
      console.log(response.uid); // Created user UID

      var updates = {};
      updates['/users/' + response.uid] = { net_worth : 5000 };
      firebase.database().ref().update(updates);
});

Here 'users' is a new table.

这篇关于Firebase在创建帐户时创建额外的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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