我在一个网站上工作,将采取的形式和显示在下表中的用户数据.我正在使用IndexedDB,所有东西都在我的笔记本电脑上本地运行,但当我将其部署到GitHub页面时,我收到以下错误: NotFoundError:无法对‘IDBDatabase’执行‘Transaction’:找不到指定的对象存储区之一.

let db;
let request = window.indexedDB.open("newDatabase", 1);
request.onupgradeneeded = function (event) {var db = event.target.result;
var objectStore = db.createObjectStore("client", {autoIncrement: true,});
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("lastName", "lastName", { unique: false });
objectStore.createIndex("email", "email", { unique: true });
objectStore.createIndex("ID", "ID", { unique: true });
objectStore.createIndex("postal", "postal", { unique: false });
objectStore.createIndex("phoneNumber", "phoneNumber", { unique: true });
var formElements = document.getElementById("form");

  var request = db
  .transaction(["client"], 'readwrite')
  .objectStore("client")
  .add({
    name: formElements[0].value,
    lastName: formElements[1].value,
    email: formElements[2].value,
    postal: formElements[3].value,
    ID: formElements[4].value,
    phoneNumber: formElements[5].value,
  });

我在网上读到,当对象store 的名称与交易中的名称不同时,可能会发生这种情况,但这里不是这样,它们都是相同的.我试着把他们换成其他名字,但问题仍然存在.

db.createObjectStore("client", {autoIncrement: true,});
.
.
.
var request = db
  .transaction(["client"], 'readwrite')

推荐答案

问题出在request.onupgradeneeded回调中,在我的实现中,我将两个客户端添加到objectStore,其中the same ID个.正如您在the ID should be unique下面的代码中看到的那样.通过添加这两个客户端,该函数抛出了一个错误,并调用了request.onerror,这就是数据库无法打开且未创建对象存储的原因.

request.onupgradeneeded = function (event) {
var db = event.target.result;
console.log("Object Store creation");
var objectstore = db.createObjectStore("client", {
    autoIncrement: true,
});

objectstore.createIndex("name", "name", { unique: false });
objectstore.createIndex("lastName", "lastName", { unique: false });
objectstore.createIndex("email", "email", { unique: true});
objectstore.createIndex("ID", "ID", { unique: true }); //HERE WAS THE PROBLEM
objectstore.createIndex("postal", "postal", { unique: false });
objectstore.createIndex("phoneNumber", "phoneNumber", { unique: true});

for (var i in clientData) {
    objectstore.add(clientData[i]); // Here was the error thrown
}
};

const clientData = [
    {
      name: "Piotr",
      lastName: "Wesoly",
      email: "PiotrWesoly@gmail.com",
      ID: 'CCU238293', //The same ID as in the other client
      postal: "90-234",
      phoneNumber: "500500200"
    },
    {
      name: "Pawel",
      lastName: "Rosiak",
      email: "pawelRosiak@gmail.com",
      ID: 'CCU238293', //The same ID as in the other client
      postal: "93-234",
      phoneNumber: "500400200"
    },
  ];

解决方法很简单:要么是set unique to false个,要么是change the ID of one of the clients个独特的东西.

Javascript相关问答推荐

如何修复内容安全策略指令脚本-SRC自身错误?

如何在使用fast-xml-parser构建ML时包括属性值?

从实时数据库(Firebase)上的子类别读取数据

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

函数返回与输入对象具有相同键的对象

当Redux提供程序访问Reduxstore 时,可以安全地从Redux提供程序外部调用钩子?

React.Development.js和未捕获的ReferenceError:未定义useState

在使用REACT更改了CSS类之后,无法更改CSS样式

匹配一个或多个可选重复的特定模式

WebSocketException:远程方在未完成关闭握手的情况下关闭了WebSocket连接.&#三十九岁;

如何更改Html元素S的 colored颜色 ,然后将其褪色为原始 colored颜色

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何向内部有文本输入字段的HTML表添加行?

需要刷新以查看Mern堆栈应用程序中的更改

使用jQuery每隔几秒钟突出显示列表中的不同单词

如何在函数组件中保留对计时器的引用

如何在底部重叠多个div?

我正在为我的单选按钮在HTML中设置一个值.使用Java脚本,我如何才能获得该值?