当尝试更新其中的记录时索引数据库使用put
方法,看起来是创造了新的价值而不是改变。根据MDN https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore.put这是更新记录的方法,但我没有得到预期的结果。有任何想法吗?这是我的代码示例。
/*
@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);
}
);