1

I'm using the jQuery-plugin jQuery-indexeddb to deal with an indexedDB.

My current problem is, that I want to add an object to an objectStore, but only if this object is not existing in another certain objectStore.

At the moment I'm using the code which follows, but the problem is that the .add function gives me an exception for every call that the object already exists, which means that the order-object which I'm generating outside of the promise for each pass of the for-loop stays the same.

What can I do to solve

Update

function fillStoreWithJson(data, store) {
var promises = [];
$.each(data,function (index,element) {
    store.add(element);
    orderIsFinished(element["AUFTRAGNR"]).done(function (result) {
        if (result === false) {
            var def = new $.Deferred();
            store.add(element).done(function (result, event) {
                def.resolve(result);                
            });
            promises.push(def);
        }
    });
});
return $.when.apply(undefined, promises).promise();

And then doing this:

fillStoreWithJson(orderJson, orderObjectStore).done(function() {
            showOrderList();
        });
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
user3507003
  • 359
  • 4
  • 17
  • Can you please show the exact `orderIsFinished` function? It currently lacks a closing `}`, and the fact that you call `promise.fail(…)` after having `return`ed isn't much reassuring either. Also, what is `getFinishedOrders`, and what is the `.get()` method its result has? – Bergi Feb 19 '15 at 18:04
  • What exactly is the error, and where is it created? The code you shown is not enough, and I don't understand what you mean by "*which means that the order-object stays the same*". Maybe you want to show us the generation code of these objects? Also, what is the expected result, how should they differ? – Bergi Feb 19 '15 at 18:07
  • Hey @Bergi, sorry for the late answer. I've corrected the oderIsFinishedMethod above. The get() method returns me a promise-object with an event which says if the operation successd or failed. the error is, that inside orderIsFinished(order["AUFTRAGNR"]).then(function(result) { the order-object stays the same, for example i have 5 orders in my data-array but in the for-loop i got 5 times the same order and so it is trying to 5 times the same order to the database and that is not possible. it is hard to explain and i dont know why it is like this. – user3507003 Feb 21 '15 at 12:34
  • Oh I see the problem now, you've forgotten to use a closure and continuously refer to the same `order` variable in all your callbacks. – Bergi Feb 21 '15 at 16:45
  • great! can you please give me an example how could this look like? thanks you very much. – user3507003 Feb 21 '15 at 17:21
  • 1
    The duplicate has many examples. Personally I recommend to use an iterator method like `.map`, `.forEach` or `$.each`. – Bergi Feb 21 '15 at 17:23
  • Ok, now i got it to the point where the objects are correct stored in the database. but now i have the problem that i want to read the database afterrwards to display the orders in html. but the method which appends the elements to a
      doesnt find something in the databse. i think the reason is, that it is executed before the entries are inserted. So i have to wait until all entries are inserted and then call the display-method. i've tried it with the above updated code but it didnt work: could you help me again please. thanks a lot.
    – user3507003 Feb 22 '15 at 11:41
  • You must not `push` the `promises` asynchronously; when you call `$.when` the array is empty and it will wait for nothing. You may want to ask a new question. Edit: Or actually not, it would be a duplicate of [this question](http://stackoverflow.com/q/20526969/1048572) – Bergi Feb 22 '15 at 13:24

1 Answers1

0

add is for adding a new record. use put for creating a new record or updating record.

Kyaw Tun
  • 12,447
  • 10
  • 56
  • 83