提问人:Richardson 提问时间:11/2/2023 最后编辑:Darren ZouRichardson 更新时间:11/3/2023 访问量:32
向 indexDB 添加值时出错
Getting an error when adding a value to indexDB
问:
当我尝试向 IndexedDB 键添加值时,我收到以下错误。我添加的对象是一个嵌套对象,但我认为我有一个流错误。我正在将 React 与 TypeScript 一起使用。
>向 IndexedDB 添加对象: DOMException: Failed to execute 'add' on 'IDBObjectStore': Evaluating the object store's key path does not generate a value
import { openDB } from 'idb';
import { ExternalNestedA, ExternalNestedB } from '../redux-store/app-types/firestoreTypesAndPredicates';
const useIndexDb = () => {
const addNestedDocToIndexDb = async (programId: string, data: ExternalNestedA | ExternalNestedB) => {
try {
const db = await openDB('indexTestDB', 1, {
upgrade(db) {
if (!db.objectStoreNames.contains('data')) {
db.createObjectStore('data', { keyPath: `${programId}` });
},
});
const transaction = db.transaction('data', 'readwrite');
const store = transaction.objectStore('data');
await store.add({ programId: data });
console.log('Object added to IndexedDB successfully.');
} catch (error) {
console.error('Error adding object to IndexedDB:', error);
}
};
return { addNestedDocToIndexDb };
};
export default useIndexDb;
答: 暂无答案
评论
'${programId}'
programId