最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error "Data provided to an operation does not meet requirements" when adding objects in indexedDB

programmeradmin5浏览0评论

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.onplete = 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");
  };
}

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.onplete = 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");
  };
}
Share Improve this question edited May 25, 2014 at 5:35 Josh 18.7k7 gold badges54 silver badges72 bronze badges asked Feb 22, 2013 at 16:10 AssassinPinguinAssassinPinguin 311 silver badge4 bronze badges 1
  • What is the value of the tarea property? if I look at the example it is a variable in your case. If that is a function or something that can't be serialized in JSON, you can't save it. – Kristof Degrave Commented Feb 25, 2013 at 12:53
Add a ment  | 

3 Answers 3

Reset to default 4

try keyPath: 'keyPath' or autoIncrement: false once you provide a "primary key" you have to set autoIncrement to false see it here

You are trying to save a DOM object. Depending on what is in there, you will or won't be able to save your data. Try leaving the tarea property out of the object and save that. And let me know what is in the tarrea property

var todo = document.querySelector('#the-do').value;
var request = objStore.add({tarea: todo, clase: "pendiente"});

It's a simple typo. The auto-increment option to the createObjectStore() method must be spelled autoIncrement (with capital I), not autoincrement.

What happens is that your object store is created without a key generator, so when adding an object it's looking for an id property as per your key path. Because the property doesn't exist you get the DataError.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论