var db;
var request = window.indexedDB.open("Reading Multi Datas", 1);
request.onupgradeneeded = function (ev) {
db = ev.target.result;
var objectStore = db.createObjectStore("users", { keyPath: "ui" });
};
request.onsuccess = function (ev) {
db = ev.target.result;
var transaction = db.transaction("users", "readwrite");
var userObjectStore = transaction.objectStore("users");
var users = [
{
ui: "p12kl58!",
firstName: "M",
latsName: "A",
age: 38,
isMarried: true,
childrens: 1,
},
{
ui: "85ph56kfg",
firsName: "A",
lastName: "B",
age: 55,
isMarried: false,
childrens: null,
},
{
ui: "6538jkg23",
firstName: "E",
lastName: "T",
age: 52,
isMarried: true,
childrens: 3,
},
{
ui: "25lp634gs",
firstName: "A",
lastName: "M",
age: 49,
isMarried: true,
childrens: 2,
},
{
ui: "6578lppo",
firstName: "L",
lastName: "T",
age: 51,
isMarried: true,
childrens: 2,
},
];
var request = userObjectStore.add(users); // Here it shows that the error may be ??
request.onsuccess = function () {
console.log("User's successfully add", request.result);
};
request.onerror = function () {
console.log("Error: ", request.error);
};
var usersData = db.transaction("users").objectStore("users");
var answer = (usersData.openCursor().onsuccess = function (ev) {
var cursor = ev.target.result;
if (cursor) {
let infoUsers = cursor.value;
console.log(infoUsers.uid);
console.log(infoUsers.firstName);
console.log(infoUsers.lastName);
console.log(infoUsers.age);
console.log(infoUsers.isMarried);
console.log(infoUsers.childrens);
cursor.continue();
}
});
};
request.onerror = function (ev) {
console.log("Error: ", ev.target.error);
};
我正在尝试创建一个 IndexedDB ,但我一直遇到同样的问题。你们中的任何人可以帮助我的建议吗?
这是例外名称:“未捕获的DOMException:无法在“IDBObjectStore”上执行“添加”:评估对象存储区的密钥路径时未产生值。at IDBOpenDBRequest.request.onsuccess”
1条答案
按热度按时间lnvxswe21#
它试图将整个数组作为一个对象添加,而出现错误消息是因为它无法找到数组本身的“ui”属性。
您需要为
users
中的每个对象调用userObjectStore.add
。没有API可以一次性批量添加对象列表。然后等待事务的“complete”事件,以了解何时完成。