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");
};
}
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"});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With