I can’t figure out what’s wrong with this snippet of code. I have an indexedDB instance. The keyPath is auto-generated. I can successfully add objects to the DB and get all objects in the DB, but I can’t successfully search for an object in an index I created.
See my jsfiddle: http://jsfiddle.net/R5ngM/13/
Haven’t nailed it perfectly yet but the issue seems to be that you’re opening the cursor on your object store, rather than the index. With the default keyPath that works fine but it won’t work when you’re trying to use a secondary index.
What I think you’re looking to do should look like:
UPDATE: I spent a lot of time looking in the wrong place after finding the above issue with the index cursor. Here’s the issue: The object you’re storing is an array not an object literal.
I was looking into the object you were storing and noticed this:
See how you’re accessing the first element of the
rowarray? You’re storing an array. Your keypath (and me) assumed an object was stored. I believe it’s possible to have an index on an array but in any case yourkeyPathis off.Here’s a largely-similar chunk of working code. I mucked it up a bit while debugging but you’ll get the gist. (It’s a nice snippit so if you don’t mind, I’ll use it as the base of other StackOverflow examples later on.)
Keep the cursor on the index as explained in my answer above. Then change to this line:
And the change this:
To this: