无法将具有复合键的对象添加到 IndexedDB objectStore。密钥已存在于对象存储中。当只有一个条目相同时
Cannot add object with composite key to IndexedDB objectStore. Key already exists in the object store. When only one entry is the same
我有一个带有以下键的 objectStore
:
当我添加具有相同productId
但不同shopName
的对象时,事务报告错误:ConstraintError: Key already exists in the object store.
。
这就是我创建 objectStore
:
的方式
openRequest.onupgradeneeded = e => {
// @ts-ignore
db = e.target.result;
const objectStore = db.createObjectStore('cart', {keyPath: ['productId', 'shopName']})
console.log('Db set up')
}
这就是我添加对象的方式:
function addProduct(product: CartProduct) {
const pk = {productId: product.productId, shopName: product.shopName}
const {transaction, objectStore, cursorRequest} = getIndexedDbTriplet();
cursorRequest.onsuccess = e => {
// @ts-ignore
const cursor = e.target.result
if (cursor) {
if (cursor.primaryKey[0] === pk.productId && cursor.primaryKey[1] === pk.shopName) {
cursor.value.count++
cursor.update(cursor.value)
// @ts-ignore
console.log(`Found ${JSON.stringify(pk)} in db and updated`)
return
}
cursor.continue()
}
objectStore.add(product)
transaction.oncomplete = () => console.log(`Added ${JSON.stringify(pk)} to db`)
}
// @ts-ignore
transaction.onerror = e => console.log(`Transaction to add product failed: ${JSON.stringify(pk)} ${e.target.error}`)
}
export class CartProduct {
readonly productId: number
readonly name: string
readonly price: number
readonly shopName: string
readonly imageUri: string
readonly weight: number
readonly count = 1
constructor(productId: number, name: string, price: number, shopName: string, imageUri: string, weight: number) {
this.productId = productId
this.name = name
this.price = price
this.shopName = shopName
this.imageUri = imageUri
this.weight = weight
}
}
export function getIndexedDbTriplet() {
const transaction = db.transaction('cart', 'readwrite')
const objectStore = transaction.objectStore('cart')
const cursorRequest = objectStore.openCursor()
return {transaction, objectStore, cursorRequest};
}
所有代码都在同一个文件中。
用 put
函数调用替换 add
解决了问题。
我有一个带有以下键的 objectStore
:
当我添加具有相同productId
但不同shopName
的对象时,事务报告错误:ConstraintError: Key already exists in the object store.
。
这就是我创建 objectStore
:
openRequest.onupgradeneeded = e => {
// @ts-ignore
db = e.target.result;
const objectStore = db.createObjectStore('cart', {keyPath: ['productId', 'shopName']})
console.log('Db set up')
}
这就是我添加对象的方式:
function addProduct(product: CartProduct) {
const pk = {productId: product.productId, shopName: product.shopName}
const {transaction, objectStore, cursorRequest} = getIndexedDbTriplet();
cursorRequest.onsuccess = e => {
// @ts-ignore
const cursor = e.target.result
if (cursor) {
if (cursor.primaryKey[0] === pk.productId && cursor.primaryKey[1] === pk.shopName) {
cursor.value.count++
cursor.update(cursor.value)
// @ts-ignore
console.log(`Found ${JSON.stringify(pk)} in db and updated`)
return
}
cursor.continue()
}
objectStore.add(product)
transaction.oncomplete = () => console.log(`Added ${JSON.stringify(pk)} to db`)
}
// @ts-ignore
transaction.onerror = e => console.log(`Transaction to add product failed: ${JSON.stringify(pk)} ${e.target.error}`)
}
export class CartProduct {
readonly productId: number
readonly name: string
readonly price: number
readonly shopName: string
readonly imageUri: string
readonly weight: number
readonly count = 1
constructor(productId: number, name: string, price: number, shopName: string, imageUri: string, weight: number) {
this.productId = productId
this.name = name
this.price = price
this.shopName = shopName
this.imageUri = imageUri
this.weight = weight
}
}
export function getIndexedDbTriplet() {
const transaction = db.transaction('cart', 'readwrite')
const objectStore = transaction.objectStore('cart')
const cursorRequest = objectStore.openCursor()
return {transaction, objectStore, cursorRequest};
}
所有代码都在同一个文件中。
用 put
函数调用替换 add
解决了问题。