使用Firebase数据库将Angular2 UID作为主键 [英] Angular2 UID as primary key using Firebase Database
问题描述
我正在使用Firebase作为Auth方法和数据库在我的应用程序中创建一个注册组件.注册新用户时,将生成一个密钥,然后将所有用户信息(包括来自身份验证的UID)保存在数据库中. 我想做的是使用身份验证中的UID作为我的主密钥,而不是Firebase生成的密钥.
Im creating a signup component in my app using Firebase as Auth method and Database. When I register a new user, a key is generated and then I save all the user info including the UID from authentication in my database. What I want to do is to use UID from authentication as my primary key instead of the key generated by Firebase.
这是我的代码:
component.ts
component.ts
signupUser(user: User) {
this.firebaseAuth.auth
.createUserWithEmailAndPassword(user.email, user.password)
.then(value => {
console.log('Success!', value);
this.afdb.list('users').push({
firstname: user.firstname,
lastname: user.lastname,
email: user.email,
uid: value.uid
});
})
.catch(err => {
console.log('Something went wrong:',err.message);
});
}
Firebase中的
json
json in Firebase
users {
-KpgNYJ0adjVyOGJiyBF {
email: test@test.com
firstname: dasdsa
lastname: dsadsads
uid: 021oxk8X6rQL6gAfC0UhZpB4vZx2
}
}
我想要的
users {
021oxk8X6rQL6gAfC0UhZpB4vZx2 {
email: test@test.com
firstname: dasdsa
lastname: dsadsads
}
}
我们将一如既往地为您提供帮助.谢谢.
Your help is deeply appreciated as always. Thanks.
我忘了用AngularFire2告诉我.
I forgot to tell Im using AngularFire2.
推荐答案
使用update
代替push
. push
创建一个新节点和该节点的唯一键.当update
更新指定的节点时,或者如果它不存在则创建它.
Use update
instead of push
. push
creates a new node and a unique key for that node. While update
updates the specified node, or creates it if it doesn't already exist.
signupUser(user: User) {
this.firebaseAuth.auth.createUserWithEmailAndPassword(user.email, user.password)
.then(value => {
console.log('Success!', value);
let userDetails = {
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
this.afdb.list('users').update(value.uid, userDetails);
})
.catch(err => {
console.log('Something went wrong:',err.message);
});
}
这篇关于使用Firebase数据库将Angular2 UID作为主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!