admin管理员组文章数量:1405607
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
3 Answers
Reset to default 4try 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
.
本文标签:
版权声明:本文标题:javascript - Error "Data provided to an operation does not meet requirements" when adding objects in indexedDB 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744294553a2599279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论