0

I have a function that adds an entry to my indexedDB database:

const dbParams = {
  // Update this when changing the db schema
  dbVersion: 1,
  // Database, object store names
  databaseName: "CropViewConfigData",
  datesConfigStoreName: "datesConfig"
};

// Create db instance
const db = new dexie(dbParams.databaseName);

// Define db schema
db.version(dbParams.dbVersion).stores({
  // First param is primaryKey followed by indexes
  [dbParams.datesConfigStoreName]: ",name, *fields"
});

// Open the database
db.open().catch(function(e) {
  console.error("Failed opening indexedDb");
});

// Store a new configuration into the database
export async function storeDateConfig(name, fields) {
  // Throw error if name is not defined
  if (!name || name === "") {
    throw new nameException("Config name is not valid");
  }
  // Throw error if no fields
  if (!fields || fields.length < 0) {
    throw new fieldsException("Fields data is not valid");
  }
  // Throw error if name already exists
  const savedConfig = await db.datesConfig.get(name);
  if (savedConfig) {
    throw new nameException("Name already exists");
  }
  // Add data to db
  await db.datesConfig.add({ fields: fields }, [name]);
}

Now according to the docs, ",name, *fields" should mean that Primary key is neither inbound nor auto-incremented, 'fields' contains an array of keys (*)

So I should be able to pass the key to the .add function?

But I am getting this error:

message:"Failed to execute 'add' on 'IDBObjectStore': The object store uses in-line keys and the key parameter was provided." name:"DataError"

What am I doing wrong?

Miha Šušteršič
  • 9,742
  • 25
  • 92
  • 163

1 Answers1

0

Check if your schema is in sync with the created database. Could it be that your schema used to have inbound keys and you've changed the code after changing the schema? What happens if you do

Dexie.delete(dbParams.databaseName).then(() => {
 ... your code
})
David Fahlander
  • 5,058
  • 1
  • 20
  • 19