首页 > 解决方案 > 通过忽略值更新索引数据库中的对象

问题描述

我写了下面的代码

updatePublication(projectName, publicationId, publicationObj, callback) {
  let self = this;
  this.initDatabase(function (db) {
    let tx = self.db.transaction(self.PUBLICATIONS, self.READ_WRITE);
    let store = tx.objectStore(self.PUBLICATIONS);
    let index = store.index(self.PROJECT_NAME);

    let request3 = index.openCursor(IDBKeyRange.only(projectName));
    console.log("hrere");
    request3.onsuccess = function () {
      let cursor = request3.result;
      if (cursor) {
        let updateObject = cursor.value;
        if (updateObject.publicationID == publicationId) {
          updateObject.publicationObject = publicationObj;
          cursor.update(updateObject);
          callback(publicationId);
        }
        cursor.continue();
      } else {
        callback(publicationId);
      }
    };
  });
}

但这给出了错误: 在此处输入图像描述

我检查了错误的原因。这是因为,传递的publicationObj 有一个名为_requestObjectBuilder 的对象,它属于订阅者类型。

在代码中的某处使用如下: _requestObjectBuilder = interval(1000).pipe(tap(() => {}));

有什么办法可以修改我的 updatePublication 代码以忽略此值?索引数据库是否支持忽略值并保存数据的查询?

注意:如果我设置publicationObj._requestObjectBuilder = undefined,数据将保存到indexedDB。但这破坏了使用 _requestObjectBuilder 的功能。

标签: javascriptindexeddb

解决方案


通过克隆对象并将其设置为未定义修复了该问题

let clonedObject = Object.assign({}, publicationObject);
clonedObject._requestObjectBuilder = undefined;

现在我正在更新 clonedObject


推荐阅读