使用 Polymerfire 时如何获取 Firebase 参考?

How to get Firebase reference when using Polymerfire?

我是 Polymer 的新手,我被困在设置数据库数据上。我设法使电子邮件身份验证有效,并且我需要在创建用户后保存用户数据。我使用 firebase-app 元素初始化应用程序。

这是重要的部分:

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);
                    });
                })

用户已成功创建,但用户数据不会被保存并且

firebase.database is not a function"

抛出错误。我想这是因为我无权访问范围内的 firebase.database 函数。我发现 如何使用纯 JavaScript 解决问题,但我不确定官方的是什么 "Polymer way"。

编辑: 我仍然无法让它工作。我设法获得了应用程序对象的引用,但似乎没有可用的数据库方法。我写了一个简单的调试函数:

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

我再次得到 "Uncaught TypeError: fb.database is not a function(…)"。

提前致谢,简

您可以在 firebase-auth 元素中获取 firebase 应用程序的引用。确保在回调函数之外执行此操作,这样您就不必处理获取 this 的正确范围的问题。如果必须,可以执行 .bind 或箭头函数。

var app = this.$.auth.app;

然后你可以做 app.database() 作为 firebase 的替代品。