0

I have a huge array called newCombs when I call store.put(game) the tab turns white and is gone in chrome task manager. Basically the process just stops and it fails to store the array

var trans = this.db.transaction('Games', 'readwrite');
var store = trans.objectStore('Games');
store.get(new Date().toLocaleDateString() + ' ' + prize.value).onsuccess = function() {
  var game = this.result;
  game.combs = game.combs.concat(newCombs); //I'm appending to the game.combs array which is empty when I first run it (when it also crashes)
  store.put(game);
}
trans.oncomplete = function(evt) {
  arrLength = 0;
  newCombs = [];
}

This is what game is equal to:

game = {
   name: new Date().toLocaleDateString() + ' ' + prize.value,
   date: new Date().toLocaleDateString(),
   prize: prize.value,
   playerData: [...],
   points: {...}
}

The above part is a method of an object so this is not the window object everything works fine until the code hits the line with: store.put(game); the page just crashes.

2
  • Are you sure this is what you think it is? this is not preserved inside anonymous functions and you're using it inside an anonymous function in the onsuccess event handler. Did you try logging the game variable?
    – paldepind
    Commented Feb 12, 2015 at 7:34
  • @paldepind this is correct in it's context it works fine I get the correct game object. Read the comment (just in case you ever get this problem) on the bottom I figured out my problem, I don't want to copy paste it here. Commented Feb 12, 2015 at 19:41

1 Answer 1

1

As @paldepind suggests you are changing scope in the onsuccess event handler and losing sight of your custom object.

One way around this would be to take advantage of a closure and assign this to a local variable, before you define the anonymous callback function:

var trans = this.db.transaction('Games', 'readwrite');
var store = trans.objectStore('Games');
var that = this;
store.get(new Date().toLocaleDateString() + ' ' + prize.value).onsuccess =     function() {
  var game = that.result;
  game.combs = game.combs.concat(newCombs); 
  store.put(game);
}
trans.oncomplete = function(evt) {
  arrLength = 0;
  newCombs = [];
}
2
  • this.result gives me the correct object. The this method on the first line is just part of my main object. I'm only showing a certain method. It's not the this that's a problem. Btw thanks I figured out what the problem was. Concat is just slow one I'm making a new array with concat that's combining with newCombs. and then replacing the existing array of game.combs. Also indexedDB is for handling huge amounts of data but not at a time. Commented Feb 12, 2015 at 19:39
  • Of course, sorry - the result is from your get, wrong end of stick ! I've found indexedDB with lots of smaller transactions. Good luck :)
    – kes
    Commented Feb 12, 2015 at 19:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.