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:
< img src =https://i.stack.imgur.com/uxaE5.pngalt =在此处输入图像说明>
这是我正在调用(在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()
使用此代码更新您的数据库
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屋!