Indexeddb adds new value instead of updating existing value

When you try to update a record in indexeddb using the method put

, a new value is generated, not a change. According to MDN , this is a method to update a record, but I don't have the expected result. Any ideas? Here is my sample code.

/*
    @keys initalisation
*/
extension.onupgradeneeded = function (event) {
    database = event.target.result;

    if (!database.objectStoreNames.contains('profile')) {

        var _profile = database.createObjectStore('profile', { autoIncrement: true });

        _profile.createIndex('default', 'default', { unique: true });
        _profile.createIndex('username', 'username', { unique: true });

        _profile.transaction.oncomplete = function (_oncompleteEvent) {

             app.database.store(database, 'profile', {
                default: true,
                username: 'my name', 
                image: 'images/svg/menu.svg' 
             });
        };
}

var app = {
    database: {
        update: function (_database, _datakeys, _key, _value, _function) {
            /*
                @pass database object, array / string, string, object, callback
            */
            var _updateRequest = _database.transaction(_datakeys, "readwrite").objectStore(_key).put(_value);

            _updateRequest.onerror = function (event) {
                try {
                    _function.error(event);
                } catch(err) {
                    console.log('An error was encountered', event.target.error.name);
                }
            };

            _updateRequest.onsuccess = function (event) {
                try {
                    _function.success(event);
                } catch (error) {
                    _function(event);
                }
            };
        }
    }
};

/*
    @how I call the function
*/

app.database.update(database, 'profile', 'profile', {default: false, username: 'hello world', image: 'http://imagepath.com' }, {
    error: function () {
        alert('failed');
    },
    success: function (returnedData) {
        console.log(returnedData);
    }
);

      

+3


source to share


1 answer


Are you using inline or standalone keys?

  • If on line you must set a property id

    (whatever you named it) on the object before using store.put

    .
  • If you are out of order, you need to pass the property value id

    as the second parameter to store.put

    .

Edit: If you don't know if the keys are in-line or out-of-order, please review below :



Each object store must have a unique name in its own database. An object store can provide a key generator and a key. If the object store has a key path, it uses built-in keys; otherwise, it uses foreign keys.

If you are using in-line and you set this property and it still does insert instead of update, try console.log('The object passed to put is %o', objToPut);

before calling store.put

and check if the property id

(or whatever you called the main store key) is defined and has the expected value (its identifier).

If you don't use a key for the store, then all puts will be inserted, because indexedDB has no way of knowing which object to update. If you want to do this anyway, perhaps you can succeed by using openCursor on the object you want to modify and then using cursor.update to replace that object.

+4


source







All Articles