JsStore bulkInsert报错咨询:IDBObjectStore执行add操作失败
Hey there, let's work through this bulkInsert issue you're facing. That DOMException about failing to execute 'add' on IDBObjectStore almost always ties to a mismatch between your bulk data and how your object store's key is configured. Let's break down the most likely fixes:
1. Ensure consistent primary key handling
If your tutors table uses an auto-incrementing primary key (like id: { primaryKey: true, autoIncrement: true }), you need to be consistent with how you include (or exclude) this key in your bulk data:
- Option 1: Omit the primary key entirely from all items (let JsStore generate it automatically):
const bulkTutors = [ { name: "Alice", subject: "Physics" }, { name: "Bob", subject: "Chemistry" } ]; db.bulkInsert({ into: "tutors", values: bulkTutors }).then(...).catch(...); - Option 2: Include a valid, unique primary key for every item (no duplicates, no
undefined/nullvalues):const bulkTutors = [ { id: 3, name: "Alice", subject: "Physics" }, { id: 4, name: "Bob", subject: "Chemistry" } ]; db.bulkInsert({ into: "tutors", values: bulkTutors }).then(...).catch(...);
The error often pops up if you mix items with and without the primary key, or if some items have invalid/undefined key values.
2. Verify key path matches your table definition
Double-check that the primary key field name in your bulk data exactly matches what's defined in your tutors table schema. For example, if your table uses tutorId as the primary key instead of id, your bulk data must use tutorId for every item—using the wrong field name will leave IndexedDB unable to resolve the key path.
3. Check for duplicate primary keys
If your primary key isn't auto-incrementing, make sure none of the items in your bulk array share the same key value. The bulkInsert method uses IndexedDB's add operation under the hood, which rejects duplicate keys. If you need to overwrite existing entries, use bulkPut instead.
4. Confirm data types match schema
Ensure the data type of your primary key (and other fields) aligns with your table definition. For example, if your primary key is defined as a number, don't pass string values like "5"—this can confuse IndexedDB when evaluating the key path.
Quick debugging tip
Print your bulk data array to the console to spot inconsistencies easily:
console.log(JSON.stringify(bulkTutors, null, 2));
This formatted output will help you catch missing keys, mismatched field names, or invalid values at a glance.
内容的提问来源于stack exchange,提问作者Kevin J Bowman

