indexeddb put not update a value, it creates a new (id)

社会主义新天地 提交于 2021-02-17 02:36:14

问题


I have a table with form, surname, lastname, email and a rectangle. I have to insert, in the rectangle, an array with arrays with points of timelines etc. I create a customer with form, surname, lastname and email, add them to indexeddb, load them later to insert the rectangle-array. After that, I want to put the newobjectstore in the indexeddb where the email is the same from my customer I choose/inserted. But with this code my array will be put in a new Objectstore with its own 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 });

回答1:


Short answer

  • Provide an identifier/key as a second parameter in objectStor.put(data, key) or
  • Use a IDBCursor und update it as stated here in the docs

Explanation

As described 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)

Your used method objectStore.put() is for insert or update tasks. If I get you right you're looking for an update - cursor.update() is your friend here (you commented out). - This is the preferred method here !

But you could do it with both methods. Say you would like to update but if the record not exists create one. In such a case the engine have to know if your record exists and what record do you try to update.

If your objectStore uses an autoIncrementing primary key the identifier of the record is not in the record itself so you have to provide the id as a second parameter to your put() function.

I find it easier to take care of the ids by myself. Then the id is part of the record (findable under the keypath you provided at objectStore-creation). Then your code could work as expected... certainly you have to add a key:value pair for an id.

Examples

// 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

If you have a primary key in your db:

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

By the way

Don't use if-else to check the completness of the transaction - it is asynchronus - if/else lies everytime here - use callbacks as I stated in my example above (onsuccess).

You should read the docs from my cite. Mozilla is a great ressource for indexedDB stuff.



来源:https://stackoverflow.com/questions/44762594/indexeddb-put-not-update-a-value-it-creates-a-new-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!