Firebase - 将额外的用户数据添加到单独的数据库 [英] Firebase - Adding additional user data to separate DB

查看:19
本文介绍了Firebase - 将额外的用户数据添加到单独的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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:

在 Firebase 用户表上添加额外详细信息

但不确定我一路上哪里出错了.这方面的文档相当薄弱,无济于事!

but not sure where i've gone wrong along the way. Documentation on this is fairly weak which doesn't help!

推荐答案

  1. firebase 对象的初始化已针对旧版本完成,请参阅此 https://firebase.google.com/docs/web/setup ,它使用 firebase.initializeApp(config) 而不是 new firebase()

  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()

使用此代码更新您的数据库与用户的附加字段

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆