IndexedDB 无法在“IDBObjectStore”上执行“添加”

zour9fqk  于 2022-12-09  发布在  IndexedDB
关注(0)|答案(2)|浏览(378)

我正在尝试向IndexedDB添加javascript对象(函数构造函数),但出现错误
无法在“IDBObjectStore”上执行“添加”:如果您有一个字符串,那么您可以使用以下方法来替换它:|“); //CHUSJ|第5区块|B......}无法克隆。
下面的代码有什么错误吗:

$.ajax({
          url: '/Review/GetDynamicPositions',
          type: 'POST',
          data: {
              ProfileId: profileId,
              DateDebut: dateDebut,
              DateFin: dateFin
          },
          
          success: function (reponse) {

              if (reponse.indexOf("Erreur") < 0) 
              {
                  review = new Review(profileId);
                  const dynamicPositions = JSON.parse(reponse);
                  dynamicPositions.forEach(dynamic => {
                      const dynamicPosition = new DynamicPosition();
                      dynamicPosition.positionX = dynamic.PositionX;
                      dynamicPosition.positionY = dynamic.PositionY;
                      dynamicPosition.floorId = dynamic.FloorId;
                      dynamicPosition.zoneId = dynamic.ZoneId;
                      dynamicPosition.mapHierachyString = dynamic.MapHierchString;

                      dynamicPosition.changeMapString();
                      review.addDynamicPosition(dynamicPosition);
                  });

                  //..Sauvegarder le review dans la BD
                  sauvegarderReview(review);
              }

          }

      });



function sauvegarderReview(review) 
{
        const transcation = db.transaction('ReviewStore', 'readwrite');
        const store = transcation.objectStore('ReviewStore');

        //..Ajouter dans la BD
        const request = store.add(review);

        request.onsuccess = function (e) 
        {
            console.log('Sauvegarder avec success');
        };

        request.onerror = function (e) {
            console.log("Error", e.target.error.name);

        };
}

//Review object 
function Review(profileId)
{
        this.profileId = profileId;
        this.dynamicPositions = [];

        this.addDynamicPosition = function (dynamicPosition) 
        {
            this.dynamicPositions.push(dynamicPosition);
        }

}

//DynamicPosition object 
function DynamicPosition() 
{
      this.positionX = 0;
      this.positionY = 0;
      this.mapHierachyString = ''; //CHUSJ>Bloc 5>B
      this.floorId = 0;
      this.zoneId = '';

      this.changeMapString = function () {
          this.mapHierachyString = this.mapHierachyString.replace(/>/g, "|"); 
          this.mapHierachyString = this.mapHierachyString.replace(/ /g, "_");
      }


}
nr9pn0ug

nr9pn0ug1#

规格说明:
每条记录都与一个值相关联。用户代理必须支持任何可序列化的对象。这包括简单类型,如String基元值和Date对象以及Object和Array示例、File对象、Blob对象、ImageData对象等。
基本上这意味着你存储到IndexedDB的对象只能包含所有可能变量的有限子集。函数是不能存储在IndexedDB中的东西之一。根据你发布的错误消息,你的review对象中一定有一个函数。
如果要将review中的数据存储在IndexedDB中,则必须对其进行某种转换,使其不包含函数或任何其他有问题的数据类型。

gzszwxb4

gzszwxb42#

您必须首先序列化数据中的JS函数(仅限),然后将它们作为序列化数据添加到IndexedDB中:

function serialize(value) {
    if (typeof value === 'function') {
        return value.toString();
    }
    if (typeof value === 'object') {
        var serializeObject = {};
        for (const [objectKey, objectValue] of Object.entries(value)) {
            console.log(`objectKey=${objectKey}  value=${objectValue}`);
            serializeObject[objectKey] = serialize(objectValue);
        }
        return serializeObject;
    }
        
    return value;
}

然后反序列化:

function deserialize(valueNew) {
    if (valueNew.toLowerCase().startsWith( 'function(' ) ) {
        return Function('"use strict";return ' + valueNew);
    }
    if (typeof valueNew === 'object') {
        var deserializeObject = {};
        for (const [objectKey, objectValue] of Object.entries(valueNew)) {
            console.log(`objectKey=${objectKey}  value=${objectValue}`);
            deserializeObject[objectKey] = deserialize(objectValue);
        }
        return deserializeObject;
    }
        
    return value;
}

在您的IndexedDB代码中,它将类似于:

function addItem(name, item) {
    var itemObject = { name: name };

    itemObject.item = serialize(item);

    var transaction = db.transaction(['store'], 'readwrite');
    var store = transaction.objectStore('store');
    var request = store.add(itemObject);

    request.onerror = function(e) {
        console.log('Error', e.target.error.name);
    };
      
    request.onsuccess = function(e) {
        console.log('Woot! added it');
    };
      
    transaction.oncomplete = function(e) {
        console.log('Woot! trans oncomplete');
    }; 
}

然后,获取密钥的keyData

deserialize(keyData);

相关问题