IndexedDB 创建数据库时使用自增的Key 更新数据库遇到的问题的一点记录
开始时是这个样子创建的数据库,更新总报错:
// 创建存储库
objectStore = db.createObjectStore(storeName, {
autoIncrement: true // 实现自增
});
后来改成这样创建, 更新终于成功了!:
// 创建存储库
objectStore = db.createObjectStore(storeName, {
keyPath:'primaryKeyName',
autoIncrement: true // 实现自增
});
按照自增KEY更新数据库的函数:
function updateDB(db, storeName, data) {
var request = db
.transaction([storeName], "readwrite") // 事务对象
.objectStore(storeName) // 仓库对象
.put(data);
request.onsuccess = function () {
console.log("数据更新成功");
};
request.onerror = function () {
console.log("数据更新失败");
};
}
调用函数更新数据库:
openDB(dbname, 1).then((db) => {
db = db
var date = new Date()
var now = date.toLocaleDateString()
var data = {
content:text,
primaryKeyName:key,
clickcontent:clickedText,
date:now
}
updateDB(db, storename, data)
})
注: 昨天发布的第一版文章在第二次使用中报错: JS indexedDB 报错:The object store uses in-line keys and the key parameter was provided. 现在改成这样就OK了!

浙公网安备 33010602011771号