gpt4 book ai didi

javascript - indexeddb 不更新值,它创建一个新的(id)

转载 作者:太空宇宙 更新时间:2023-11-04 15:31:14 24 4
gpt4 key购买 nike

我有一个表格,其中包含表格、姓氏、姓氏、电子邮件和一个矩形。我必须在矩形中插入一个包含时间线点等数组的数组。我创建一个包含表单、姓氏、姓氏和电子邮件的客户,将它们添加到 indexeddb,稍后加载它们以插入矩形数组。之后,我想将 newobjectstore 放入 indexeddb 中,其中的电子邮件与我选择/插入的客户的电子邮件相同。但通过这段代码,我的数组将被放入一个具有自己 ID 的新对象存储中。

function cmd_SaveRec(hypeDocument, elementID)
{
hypeDocument.getElementById('rectext').innerHTML = hypeDocument.getElementById('beschriftung').value;

var store_cust = db.transaction(["customer"], "readwrite").objectStore("customer").index("rectangle");

var cursorReq = store_cust.openCursor();
cursorReq.onsuccess = function (e) {
cursor = e.target.result;
if(cursor) {
if(cursor.value.email == mailAdd)
{
//cursor.update([rec_ar]);
if(store_cust.objectStore.put({rectangle: [rec_ar]}))
{
console.info(store_cust.objectStore);
console.info('Gespeichert');
alert("Gespeichert");
} else {
console.info('cmd_SaveRec::Problem');
}
}
cursor.continue();
}
};

cursorReq.onerror = function(e) {
console.log("Error");
console.dir(e);
}
}

var store_cust = evt.currentTarget.result.createObjectStore(
DB_STORE_NAME_CUSTOMER, { keyPath: 'cust_id', autoIncrement: true });

store_cust.createIndex('form', 'form', { unique: false }); //
store_cust.createIndex('surname', 'surname', { unique: false });
store_cust.createIndex('lastname', 'lastname', { unique: false });
store_cust.createIndex('email', 'email', { unique: true });
store_cust.createIndex('rectangle', 'rectangle', { unique: false, multiEntry: true });

最佳答案

简短回答

  • 提供标识符/ key 作为 objectStor.put(data, key) 中的第二个参数
  • 使用 IDBCursor 并更新它 as stated here in the docs

说明

如文档中所述:

The put() method of the IDBObjectStore interface updates a given record in a database, or inserts a new record if the given item does not already exist. (source developer.mozilla.org)

您使用的方法objectStore.put()用于插入或更新任务。如果我没猜错的话,您正在寻找更新 - cursor.update() 是您的 friend (您已注释掉)。 - 这是这里的首选方法!

但这两种方法都可以做到。假设您想要更新,但如果记录不存在,请创建一个。在这种情况下,引擎必须知道您的记录是否存在以及您尝试更新哪些记录。

如果您的 objectStore 使用自动增量主键,则记录的标识符不在记录本身中,因此您必须提供 ID作为 put() 函数的第二个参数。

我发现自己处理 ids 更容易。那么 id 就是记录的一部分(可以在您在 objectStore-creation 中提供的 keypath 下找到)。 然后您的代码可以按预期工作...当然您必须为 id 添加键:值对。

示例

// create a store with ids YOU handle e.g.
var request = indexedDB.open(dbname, dbversion+1);
request.onerror = errorHandler;
request.onupgradeneeded = function(event){
var nextDB = request.result;
if (!nextDB.objectStoreNames.contains('account')) {
nextDB.createObjectStore('account', {keyPath: "id", autoIncrement: true});
}
}

// Your record has to llok like this
{id: 123456789, rectangle: [rec_ar]}

// Now your code above should work

如果您的数据库中有主键:

store_cust.objectStore.put({rectangle: [rec_ar]}, PRIMARY_KEY)
// where PRIMARY_KEY is the id of this specific record

顺便说一句

不要使用 if-else 来检查事务的完整性 - 它是异步的 - if/else 每次都在这里 - 使用回调,如我在上面的示例中所述(onsuccess )。

您应该阅读我引用的文档。 Mozilla 是索引数据库内容的绝佳资源。

关于javascript - indexeddb 不更新值,它创建一个新的(id),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44762594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com