Angular2 UID 作为主键使用 Firebase 数据库 [英] Angular2 UID as primary key using Firebase Database
问题描述
我使用 Firebase 作为身份验证方法和数据库在我的应用中创建了一个注册组件.当我注册一个新用户时,会生成一个密钥,然后我将所有用户信息(包括来自身份验证的 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);
});
}
这篇关于Angular2 UID 作为主键使用 Firebase 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!