3

When i try to add the object to the objectStore with .add, the console shows this error: DataError: Data provided to an operation does not meet requirements. If someone can tell where this error came from it will really help me. This is the code:

var request = objStore.add({tarea: todo, clase: "pendiente"});

var db;

function create_to_do(){
  var todo = document.querySelector('#the-do').value;
  var transaction = db.transaction("to_do", "readwrite");
  transaction.oncomplete = function(eve){
    console.log("all done¡")
  }

  transaction.onerror= function(eve){
    console.log("something went wrong: "+ eve.target.errorCode);
  }; 

  var objStore = transaction.objectStore("to_do");
  var request = objStore.add({tarea: todo, clase: "pendiente"});
  request.onsuccess = function(eve){
    console.log("all done¡");
    console.log(eve.target.result);
  };
}

function indexDB(){
  var request = indexedDB.open('todos', 1);
    request.onsuccess = function (evt) {     
    db = this.result;
    console.log("Database Opened");
  };

  request.onerror = function (evt){
    console.log("OpenDB error: " + evt.target.errorCode);
  };

  request.onupgradeneeded = function(evt){
    store = evt.currentTarget.result.createObjectStore("to_do", 
              {keyPath: 'id', autoIncrement: true});
    store.createIndex('clase', 'clase', {unique: false});
    console.log("index created");
  };
}
4

3 回答 3

4

尝试keyPath: 'keyPath'autoIncrement: false一旦您提供“主键”,您必须将 autoIncrement 设置为 false在这里查看

于 2013-02-23T08:39:51.327 回答
3

您正在尝试保存 DOM 对象。根据其中的内容,您将能够或不能保存数据。尝试将 tarea 属性保留在对象之外并保存。让我知道 tarrea 财产中有什么

var todo = document.querySelector('#the-do').value;
var request = objStore.add({tarea: todo, clase: "pendiente"});
于 2013-02-25T12:57:11.280 回答
0

这是一个简单的错字。该createObjectStore()方法的自动增量选项必须拼写autoIncrement(大写 I),而不是autoincrement.

发生的情况是您的对象存储是在没有密钥生成器的情况下创建的,因此在添加对象时,它会id根据您的密钥路径查找属性。因为该属性不存在,所以您得到DataError.

于 2013-02-22T16:36:06.507 回答