简体   繁体   English

Firebase-将其他用户数据添加到单独的数据库

[英]Firebase - Adding additional user data to separate DB

I'm new to Firebase and am using the email/password sign-in method. 我是Firebase的新手,正在使用电子邮件/密码登录方法。 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. 我从电子邮件/密码注册方法的理解是,任何其他字段都必须与用户注册时如何存储电子邮件/密码身份验证分开存储。

I have created in my databases a sub-section to store additional user details called users. 我在数据库中创建了一个小节来存储称为用户的其他用户详细信息。 here's how it looks in firebase: 这是它在Firebase中的外观:

在此处输入图片说明

Here is the function i'm calling (in React) to create a new user: 这是我正在调用(在React中)创建新用户的函数:

    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: 遵循了我在这里找到的另一个示例:

Add Extra Details on Firebase User Table 在Firebase用户表上添加其他详细信息

but not sure where i've gone wrong along the way. 但不确定我一路上哪里出错了。 Documentation on this is fairly weak which doesn't help! 关于此的文档非常薄弱,无济于事!

  1. 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 of new firebase() firebase对象的初始化已针对旧版本完成,请参阅此https://firebase.google.com/docs/web/setup ,它使用firebase.initializeApp(config)代替了new firebase()

  2. to update your database with user's additional fields use this code 使用用户的其他字段更新数据库,请使用此代码

     firebase.database().ref('users/' + user.uid).set({ firstName: firstName, lastName: lastName }) 

For updating your database with additional parameters use the below given code and make changes accordingly. 要使用其他参数更新数据库,请使用以下给定的代码并进行相应的更改。

function writeUserData(userId, name, email, imageUrl) {
                firebase.database().ref('users/' + userId).set({
                username: name,
                email: email,
                profile_picture : imageUrl
                });
              }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM