Adam
Adam

Reputation: 632

Accessing the primary key in IndexedDB

I am trying to print the objects primary key in IndexedDB to the console.

I've seen two examples that use 'Cursor.Value.Key' but when I use that I get 'undefined'.

if (cursor) 
    {
        console.log(cursor.value.key);
        console.log(cursor.value.username);
        console.log(username)
        console.log(cursor.value.password);
        console.log(password)

This is my database

objectStore = db.createObjectStore('users', { keyPath: "id", autoIncrement: true });
    objectStore.createIndex('username', 'username', { unique: true });
    objectStore.createIndex('password', 'password', {unique: false});

It should print the objects primary key, ie 1 or 2

Upvotes: 3

Views: 1625

Answers (1)

dumbmatter
dumbmatter

Reputation: 9653

Try cursor.key (or cursor.primaryKey for an index rather than an object store) or cursor.value.id. cursor.value is the object itself, so cursor.value.key would only return the primary key if you had keyPath set to "key".

Upvotes: 3

Related Questions