Firebase - 将额外的用户数据添加到单独的数据库 [英] Firebase - Adding additional user data to separate DB
问题描述
我是 Firebase 的新手,正在使用电子邮件/密码登录方法.我的方法工作正常,但是在我的注册表单上,我有其他要输入数据库的字段.我对电子邮件/密码注册方法的理解是,任何其他字段都必须与用户注册时电子邮件/密码身份验证的存储方式分开存储.
I'm new to Firebase and am using the email/password sign-in method. I have the method working fine however on my signup form I have additional fields I want to enter in the database. My understanding from the email/password signup method is that any additional fields will have to be stored separately to how the email/ password auth is stored when a user signs up.
我在我的数据库中创建了一个子部分来存储称为用户的其他用户详细信息.这是它在 Firebase 中的样子:
I have created in my databases a sub-section to store additional user details called users. here's how it looks in firebase:
这是我调用(在 React 中)创建新用户的函数:
Here is the function i'm calling (in React) to create a new user:
signUp(e) {
e.preventDefault();
var firstName = $('.signup-first-name').val();
var lastName = $('.signup-last-name').val();
var userName = $('.signup-user-name').val();
var password = $('.signup-user-password').val();
var email = $('.signup-email').val();
var auth = firebase.auth();
const promise = auth.createUserWithEmailAndPassword(email,password).then(function(user) {
var user = firebase.auth().currentUser;
user.updateProfile({
displayName: userName,
}).then(function() {
// Update successful.
// new db code here
var ref = new firebase("https://music-app-7a4d3.firebaseio.com");
ref.onAuth(function(authData) {
ref.child("users").child(authData.uid).set({
firstName: firstName,
lastName: lastName
})
})
// end new db code here
}, function(error) {
// An error happened.
});
}, function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
console.error(error);
}
});
promise.catch(e => console.log(e.message));
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser) {
console.log(firebaseUser)
} else {
console.log("not logged in")
}
});
}
遵循我在此处找到的另一个示例:
Have followed another example I found on here:
但不确定我一路上哪里出错了.这方面的文档相当薄弱,无济于事!
but not sure where i've gone wrong along the way. Documentation on this is fairly weak which doesn't help!
推荐答案
firebase 对象的初始化已针对旧版本完成,请参阅此 https://firebase.google.com/docs/web/setup ,它使用
firebase.initializeApp(config)
而不是new firebase()
Initialization of firebase object was done for the old version, see this https://firebase.google.com/docs/web/setup , it uses
firebase.initializeApp(config)
instead ofnew firebase()
使用此代码更新您的数据库与用户的附加字段
to update your database with user's additional fields use this code
firebase.database().ref('users/' + user.uid).set({
firstName: firstName,
lastName: lastName
})
这篇关于Firebase - 将额外的用户数据添加到单独的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!