1
votes

I am new to Polymer and I am stuck on setting the database data. I manged to make email authentication work and I need to save user data after user creation. I initialize the app with firebase-app element.

Here is the important part:

this.$.auth.createUserWithEmailAndPassword(email, pass).then(function (user) {
                    user.sendEmailVerification();
                    document.getElementById("emaildialog").toggle();
                    var view = document.getElementById("r_view");

                    firebase.database().ref('/user/' + user['uid']).set({
                        name: view.name,
                        surname: view.surName
                    }).catch(function (err) {
                        console.log(err.message);
                    });
                })

User is successfully created but the user data won't get saved and

firebase.database is not a function"

error is thrown. I guess it's because I don't have access to firebase.database function in the scope. I found many ways how to solve the issue using pure JavaScript, but I'm not sure what is the official "Polymer way".

EDIT: I still can't get it to work. i managed to get a reference of app object but it seems like there is no database method available. I wrote a simple function for debugging:

    debugFunction: function () {
        if (!!this.user) {
            var fb = this.$.auth.app;
            console.log(!!fb); // Output is true,
            var database = fb.database(); 
        }
    }

I get the "Uncaught TypeError: fb.database is not a function(…)" once more.

Thanks in advance, Jan

1

1 Answers

2
votes

You can get the reference of the firebase app inside your firebase-auth element. Make sure you do this outside of the callback function so you won't have to deal with getting the proper scope of this. If you must, you can do .bind or arrow functions.

var app = this.$.auth.app;

Then after that you can do app.database() as a replacement for the firebase one.