Polymerfire - 在事件发生后创建数据

Polymerfire - creating data after an event

我正在使用来自 bower bower install firebase/polymerfire

的 firebase/polymerfire 包

如何在触发方法后在数据库中创建一些数据?

文档标签看起来会显示和更新数据。我想在用户注册时创建一些数据供用户使用。

app.signInAnonymously = function() {
        this.error = null;
        this.$.auth.signInAnonymously();
        // add default data for the user template
      };

如何像普通 SDK 一样使用默认的 set() 或 push 方法?

如何在 JavaScript 的事件中调用它?

尝试将路径绑定到我的文档时,例如

<firebase-document
 path="/"
 data="{{firebaseData}}">
</firebase-document>


 {{firebaseData}}

数据不会显示,但我有身份验证。

您实际上可以在那里直接使用 firebase api,因为 firebase-auth 已经包含它,但是如果您想保留基于元素的功能,您可以这样做:

添加一个空 firebase-document 到您的 template

<firebase-document id="mydoc"></firebase-document>

然后在你的元素中调用它的save函数

app.signInAnonymously = function() {
    this.error = null;
    this.$.auth.signInAnonymously();
    // add default data for the user template

    //set path to null somewhere to avoid overwriting data, I recommend doing it here since save's path update is lazy
    this.$.mydoc.path = null;

    //set data to the value to set/push
    this.$.mydoc.data = {/*your data*/};

    //call save, if the second parameter is falsey it'll call push to the first parameter if it's truthy it'll set the data to firstparam/secondparam
    this.$.mydoc.save('path/to/users', userid);
  };

关于使用 firebase-document 获取数据,请检查您的数据库中是否确实有数据以及您的安全规则,如果您仍然看不到您的数据,则可能与 this issue 有关, 请记住 polymerfire 仍处于预发布状态