Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript indexeddb put不更新值,它会创建一个新的(id)_Javascript_Arrays_Indexeddb - Fatal编程技术网

Javascript indexeddb put不更新值,它会创建一个新的(id)

Javascript indexeddb put不更新值,它会创建一个新的(id),javascript,arrays,indexeddb,Javascript,Arrays,Indexeddb,我有一张表格,上面有表格、姓氏、姓氏、电子邮件和一个矩形。我必须在矩形中插入一个数组,数组中包含时间点等。我创建了一个带有表单、姓氏、姓氏和电子邮件的客户,将其添加到indexeddb,稍后加载以插入矩形数组。之后,我想将newobjectstore放在indexeddb中,其中的电子邮件与我选择/插入的客户的电子邮件相同。但是通过这段代码,我的数组将被放入一个具有自己ID的新Objectstore中 function cmd_SaveRec(hypeDocument, elementID) {

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

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

简短回答
  • objectStor.put(数据,键)
    中提供标识符/键作为第二个参数
  • 使用IDB游标并更新它
解释 如文件所述:

IDBObjectStore接口的put()方法更新数据库中的给定记录,如果给定项不存在,则插入新记录。()

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

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

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

我发现自己处理身份证比较容易。然后id是记录的一部分(可在objectStore创建时提供的
keypath
下查找)然后您的代码可以按预期工作。。。当然,您必须为id添加一个key:value对

例子 如果数据库中有主键:

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是indexedDB的优秀资源。

您能展示创建对象存储/索引的代码吗?我猜您正在使用一个键生成器(
{autoIncrement:true}
),如果是这样,答案是使用键调用
put()
store\u cust.objectStore.put({rectangle:[rec\u ar]}),cursor.primaryKey)
-但是
update()
也可以用来做同样的事情。复制了SaveRec函数下的createObjectStore。但是不,您的代码存储区\u cust.objectStore.put({rectangle:[rec\u ar]}),cursor.primaryKey)也会执行相同的操作,就像我的代码一样。它将数组保存在新对象中。
// 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