6

my return json file looks like this:

var data = [{"col1":"value1","col2":"value1","col3":"value1"},{"col1":"value2","col2":"value2","col3":"value2"},{"col1":"value3","col2":"value3","col3":"value3"}];

without JSON.stringify data looks like this:

[object Object],[object Object],[object Object]

but with it the result.length is not 5 but the total number of characters of the string and that way I cant do the loop

var result = JSON.stringify(data);
for(i=0; i<result.length; i++){
var transaction = db.transaction([STORE], IDBTransaction.READ_WRITE);
var put = transaction.objectStore(STORE).put(result);
};   
1
  • Don't stringify and In line 4 put(result[i]) Commented Feb 4, 2022 at 6:09

2 Answers 2

5
var data = [{"col1":"value1","col2":"value1","col3":"value1"},{"col1":"value2","col2":"value2","col3":"value2"},{"col1":"value3","col2":"value3","col3":"value3"}];

If you are trying to store each OBJECT, then don't stringify it or anything, it is already in perfect form. Change your for() loop to loop through the data objects.

Kristof Degrave had a good point to put these outside of the actual for loop for performance reasons.

    var transaction = db.transaction([STORE], IDBTransaction.READ_WRITE); 
    var objstore = transaction.objectStore(STORE); 

    for (i = 0; i < data.length; i++) { 
        objstore.put(data[i]);
    } 
Sign up to request clarification or add additional context in comments.

8 Comments

Yes, working!!! (I forgot to write the "[i]" in here) I was trying everything like: eval(), JSON.stringify() and JSON.parse() and didn't try it direct and with the loop... Thanks a lot mcpDESIGNS! Cheers.
I would pull the transaction and the object store part outside the for loop, this will increase the performance.
hum... what do you mean Kristof? like calling an external function inside the loop to add each object? wouldn't that be even more circular?
Yeah it sounds like it needs each object inserted one at a time right?
Now you are creating a transaction and an objectstore object for every object you want to store. If you do this outside the for loop you will create it once and do the puts all in one transaction. Do it like this: var transaction = db.transaction([STORE], IDBTransaction.READ_WRITE); var objstore = transaction.objectStore(STORE); for(i = 0; i < data.length; i++){ objstore.put(data[i]); }
|
5

For new visitors, suggesting a tad of modification: IDBTransaction.READ_WRITE has been deprecated so use "readwrite" instead.

Resource: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB Reference:

Older experimental implementations use the deprecated constant IDBTransaction.READ_WRITE instead of "readwrite".

Also, to reduce the loc (which I mostly prefer), use:

var objstore = db.transaction([STORE], "readwrite").objectStore(STORE); 
for (i = 0; i < data.length; i++) { 
    objstore.put(data[i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.