Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
  };
}
like image 474
AssassinPinguin Avatar asked Sep 14 '25 08:09

AssassinPinguin


2 Answers

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

like image 115
Ruben Teixeira Avatar answered Sep 17 '25 00:09

Ruben Teixeira


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"});
like image 44
Kristof Degrave Avatar answered Sep 17 '25 01:09

Kristof Degrave