lin*_*say 3 html javascript storage indexeddb
当尝试使用该方法更新indexeddb 中的记录时put,似乎创建了一个新值而不是更改。根据MDN,这是更新记录的方法,但我没有得到预期的结果。有任何想法吗?这是我的代码示例。
/*
@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);
}
);
Run Code Online (Sandbox Code Playgroud)
您使用的是内联键还是外键?
id在使用store.put.id属性值作为第二个参数传递给store.put。编辑:如果您不知道这些键是内联的还是外联的,请查看以下内容:
每个对象库都必须有一个在其数据库中唯一的名称。对象存储可以选择有一个密钥生成器和一个密钥路径。如果对象存储有键路径,则使用内联键;否则,它使用的是外联键。
如果您正在使用内联,并且您正在设置属性,并且它仍在执行插入而不是更新,请console.log('The object passed to put is %o', objToPut);在调用之前尝试store.put,并验证是否定义了该id属性(或您命名为商店主键的任何内容)并具有预期值(其 id)。
如果您没有为存储使用键,那么所有放置都将是插入,因为 indexedDB 无法知道要更新哪个对象。如果您无论如何都想这样做,也许您可以在要更改的对象上使用 openCursor ,然后使用 cursor.update 替换该对象。
| 归档时间: |
|
| 查看次数: |
1970 次 |
| 最近记录: |