5
votes

I've looked at other similar questions. My case is different because I am inserting simple json objects with no keys, no auto increment.

Here's the part that does the insert:

function insertInto(model, data, successCallback) {
    console.log("inserting into model "+model); 
    var transaction = db.transaction([model], IDBTransaction.READ_WRITE || 'readwrite');
    var store, i, request;
    var total = data.length;

    function successCallbackInner() {
        total = total - 1;
        if (total === 0) {
            successCallback();
        }
    }
    transaction.onerror = indexedDBError;
    store = transaction.objectStore(model);
    for (i in data) {
        if (data.hasOwnProperty(i)) {
            console.log(data[i]);
            request = store.add(data[i]);
            request.onsuccess = successCallbackInner;
            request.onerror = indexedDBError;
        }
    }
}

The error is DataError: Data provided to an operation does not meet requirements.

When I log the data I'm trying to insert it confirms my object is a simple object with three string parameters: Object { cooked="well done", cheese="provolone", toasted="no"}

Here is a jsFiddle with the full testing code.

Any and all suggestions are appreciated. Even "try this" replies. I've been trying to figure this out since yesterday morning.

Thank you

1

1 Answers

12
votes

If you try it in Chrome you'll see a more informative error:

DataError: Failed to execute 'add' on 'IDBObjectStore': The object store uses out-of-line keys and has no key generator and the key parameter was not provided.

In the jsfiddle, your object stores are being created with db.createObjectStore(modelData[i].name, {autoIncrement: false}); which means (1) there is no key path (keyPath option not present), and (2) there is no key generator (autoIncrement option is false). That means that (1) the keys aren't pulled from the value, and (2) the store won't generate keys for you. So you need to specify keys when calling add() or you'll get that exception.

A few examples:

// out-of-line keys, no key generator
var s1 = db.createObjectStore('s1');
s1.add(value, "my_key"); // key will be "my_key"

// out-of-line keys, key generator
var s2 = db.createObjectStore('s2', {autoIncrement: true});
s2.add(value); // key will be 1
s2.add(value, 123); // key will be 123
s2.add(value); // key will be 124

// in-line keys, no key generator
var s3 = db.createObjectStore('s3', {keyPath: 'id'});
s3.add({id: 123}); // key will be 123

// in-line keys, key generator
var s4 = db.createObjectStore('s4', {keyPath: 'id', autoIncrement: true});
s4.add({}); // key will be 1 (and inserted into record)
s4.add({id: 123}); // key will be 123
s4.add({}); // key will be 124 (and inserted into record)

Cases not shown would throw a DataError exception.