Indexeddb添加新值而不是更新现有值 [英] Indexeddb adds a new value instead of updating an existing value

查看:151
本文介绍了Indexeddb添加新值而不是更新现有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 put 方法更新 indexeddb 中的记录时,似乎创建了一个新值而不是更改。根据 MDN 是更新记录的方法,但我没有预期的结果。有任何想法吗?这是我的代码示例。

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);
    }
);


推荐答案

您使用的是在线还是不在线线路密钥?

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


  • 如果是在线,则需要设置 id (在使用 store.put 之前对象中的属性。

  • 如果是外行的,则需要通过 id 属性值作为 store.put 的第二个参数。

  • 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.

编辑:如果您不知道密钥是在线还是在线,请查看关注

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


每个对象store必须具有在其数据库中唯一的名称。对象存储可以可选地具有密钥生成器和密钥路径。如果对象存储库具有密钥路径,则它使用内联密钥;否则,它使用外线密钥。

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.

如果您正在使用内联,并且您正在设置该属性,它仍在执行插入而不是更新,在调用<$ c $之前尝试 console.log('传递给put的对象是%o',objToPut); c> store.put ,并验证是否定义了 id 属性(或任何您命名为商店主键的属性)并具有预期效果value(它的id)。

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).

如果你没有使用商店的密钥,那么所有的put都会被插入,因为indexedDB无法知道哪个对象是更新。无论如何你想要这样做,也许你可以在你想要改变的对象上使用openCursor,然后使用cursor.update来替换那个对象。

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.

这篇关于Indexeddb添加新值而不是更新现有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆