Polymer + Firebase:firebase-document,数据被覆盖?

Polymer + Firebase: firebase-document, data being overwritten?

我想使用 firebase-document 将数据保存到我的 firebase。数据似乎已正确保存,但立即被覆盖。这是我得到的错误...

console.log

Sync to memory.
Updating data from Firebase value: Object {comment: "foo", date: "bar", id: "-XXXxxxxxxXXXXXXxxxx", merchant: "baz", status: "bat"…}
app-storage-behavior.html:368

Sync to memory.
app-storage-behavior.html:350
Updating data from Firebase value: Object {date: ""}

看到日志中的第二段了吗?这是我不希望看到的。是什么原因造成的?我可以做些什么来实现我想要的行为?这是我的代码...

x-el.html
<firebase-document
    id="document"
    app-name="my-firebase"
    data="{{editableItem}}"
    log>
</firebase-document>
...
save: function() {
  return this.$.document.save(this.itemsPath);
}

编辑

这是控制台日志的屏幕截图。

查看第二个同步到内存日志。它描述了将本质上是空对象的内容:{date:""} 写入内存 (firebase)。此操作覆盖 先前的对象:{comment: "c", date: "", merchant: "c", status: "new", total: "3"} 写入同一位置。

Here is the firebase-document.html file in question.

下面是代码的相关部分。

firebase-document.html
attached: function() {
  this.__refChanged(this.ref, this.ref);
},

detached: function() {
  if (this.ref) {
    this.ref.off('value', this.__onFirebaseValue, this);
  }
},

observers: [
  '__refChanged(ref)'
],

...

__refChanged: function(ref, oldRef) {
  if (oldRef) {
    oldRef.off('value', this.__onFirebaseValue, this);
  }
  if (ref) {
    ref.on('value', this.__onFirebaseValue, this);
  }
},

__onFirebaseValue: function(snapshot) {
  var value = snapshot.val();
  if (value == null) {
    value = this.zeroValue;
  }
  if (!this.new) {
    this.async(function() {
      this.syncToMemory(function() {
        this._log('Updating data from Firebase value:', value); // Causes overwrite
        this.set('data', value);
      });
    });
  }
}

this.$.document.save(this.itemsPath) 的副作用是 firebase-documentpath 将被重置为指向您在数据库中创建的新项目。但是,此代码作为 path 单向绑定到 firebase-document 中,因此每次保存数据时,您都会将组件重新指向数据库中没有数据的位置。双向绑定 and/or 根本没有绑定 path,或者在 save(parentPath, key) 中使用显式 key 以便目标匹配 editableItemId 应该可以解决您的问题。

我之前遇到过与 'Sync to memory' 类似的问题。我所做的是直接使用 firebase.database 函数(如添加和设置)进行更改,并且从不尝试对 中的数据进行更改。而我使用 < firebase-query > 代替。如所述:https://firebase.googleblog.com/2014/05/handling-synchronized-arrays-with-real.html

我们必须将下载的 data/array 视为只读。并使用 firebase.database 函数直接在 firebase 中进行更改。以这种方式 监听并更新自己,这是一个循环。但是,为了使用 ID 修改特定更改,您可以使用来自 的添加的 arrayfromquery[0].$key 数据。我想这在某种程度上并不完全相关,但我希望它能有所帮助:)