Firebase在创建帐户时创建额外的用户数据
问题描述:
我正在使用Firebase和Angular 2创建新用户。我想通过为每个新创建的用户添加更多数据来扩展这一点。在这个例子中,我希望每个新创建的用户都以net worth = 5000
的指定属性开始。所以,我的架构将类似于:Firebase在创建帐户时创建额外的用户数据
email | ''
password | ''
net worth | '5000'
通过Firebase docs已经走了,我已经找到了save data功能。但是,我不确定如何合并set
功能与我的用户名和密码验证脚本。
下面是基本注册脚本。
signup(){
firebase.auth().createUserWithEmailAndPassword('[email protected]', 'ucantcme')
.catch(function(error){
console.log(error);
});}
我想知道如何实现这个注册的set
功能,这样我可以为用户增加默认数据。
答
您无法在auth区域保存'net_worth'。为此,你需要在数据库中有一个独立的表。例如'用户'。检查下面的代码。
用户创建后,您将获得UID。使用该UID将相关数据存储到数据库中。
firebase.auth().createUserWithEmailAndPassword('[email protected]', '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);
});
这里'用户'是一个新表格。