Indexeddb adds a new value instead of updating an existing value

╄→尐↘猪︶ㄣ 提交于 2019-12-07 18:12:35

问题


When attempting to update a record within indexeddb using the put method, it appears that a new value is created rather than a change. According to MDN this is the method to update a record, yet I'm not having the expected result. Any ideas? Here is my code example.

/*
    @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);
    }
);

回答1:


Are you using in-line or out-of-line keys?

  • If in-line, you need to set the id (whatever you named it) property in the object before using store.put.
  • If out-of-line, you need to pass the id property value as the second parameter to store.put.

Edit: if you have no idea whether the keys are in-line or out-of-line, review the following:

Every object store must have a name that is unique within its database. The object store can optionally have a key generator and a key path. If the object store has a key path, it is using in-line keys; otherwise, it is using out-of-line keys.

If you are using in-line, and you are setting the property, and it is still performing an insert instead of an update, try console.log('The object passed to put is %o', objToPut); before calling store.put, and verifying whether the id property (or whatever you named the primary key of the store) is defined and has the expected value (its id).

If you are not using a key for the store, then all puts will be inserts, because indexedDB has no way to of knowing which object to update. If you want to do that anyway, maybe you could get away with using openCursor on the object you want to change and then using cursor.update to replace that object.



来源:https://stackoverflow.com/questions/25130117/indexeddb-adds-a-new-value-instead-of-updating-an-existing-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!