Accessing the primary key in IndexedDB

Viewed 916

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

1 Answers

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".

Related