lundi 2 octobre 2017

Firebase Web JS: How to write data according to userID (uid)? | users > uid > {name, emails etc}?

I really tired of this firebase web app. Briefly, I can not write on firebasedatabase any data that depends on "USER ID/(user.uid)". The code snippet that below doesn't work. What's wrong?

I get this error; "Uncaught ReferenceError: user is not defined at index.html:206 (anonymous) @ index.html:206

<script>
    var config = {
        apiKey: 
        authDomain: 
        databaseURL: 
        projectId: 
        storageBucket: 
        messagingSenderId: 
    };
    firebase.initializeApp(config);

    initApp = function () {
        firebase.auth().onAuthStateChanged(function (user) {
            if (user) {
                // User is signed in.
                var email = user.email;
                var uid = user.uid;
                var providerData = user.providerData;
                user.getIdToken().then(function (accessToken) {
                    document.getElementById('LogOut').textContent = 'Log out';
                    var DT = JSON.stringify({
                        displayName: displayName,
                        email: email,
                        emailVerified: emailVerified,
                        phoneNumber: phoneNumber,
                        photoURL: photoURL,
                        uid: uid,
                        accessToken: accessToken,
                        providerData: providerData
                    }, null, '  ');

                    console.log(DT);

                });
            } else {
                // User is signed out.
                document.getElementById('LogOut').textContent = 'Log in';
            }
        }, function (error) {
            console.log(error);
        });
    };


    function signOut() {
        firebase.auth().signOut();
    }
    document.getElementById("LogOut").addEventListener("click", signOut);

    window.addEventListener('load', function () {
        initApp()


    });

    var userId = user.uid;
    var email = user.email;

    function writeUserData(userId, email) {
        firebase.database().ref('users/' + userId).set({
            username: userId,
            email: user.email

        });
    }

    document.getElementById("submitBtn").addEventListener("click", writeUserData);

</script>




Aucun commentaire:

Enregistrer un commentaire