Skip to content

Table.get()

David Fahlander edited this page Nov 25, 2016 · 15 revisions

Syntax

// Dexie 1.x and 2.x:
table.get(primaryKey, callback);

// Dexie 2.x only:
table.get({keyPath1: value1, keyPath2: value2, ...}, callback);

Parameters

primaryKey Primary key of object to get
callback: Function function (item) { } optional
{keyPath1: value1, keyPath2: value2, ...} Criterias to filter

Callback Parameters

item: Object Found item if any, otherwise undefined.

Return Value

Promise

Remarks

Fetches object of given primaryKey or where given criteras ({keyPath1: value1, keyPath2: value2}) are fulfilled and returns the first matching result.

If callback is omitted and operation succeeds, returned Promise will resolve with the result of the operation, calling any Promise.then() callback.

If callback is specified and operation succeeds, given callback will be called and the returned Promise will resolve with the return value of given callback.

If operation fails, returned promise will reject, calling any Promise.catch() callback.

Sample

db.friends.get(1, function (firstFriend) {
    alert ("Friend with id 1: " + firstFriend.name);
});

db.friends.get(1).then (function (firstFriend) {
    alert ("Friend with id 1: " + firstFriend.name);
});

db.friends.get({firstName: "Austin", lastName: "Powers"}, austin => {
    return db.vehicles.where({owner: austin.id}).toArray(austinsVehicles => {
        austin.vehicles = austinsVehicles;
        return austin;
    });
}).then (austinWithVehicles => {
    //..
});

async function foo() {
    let firstFriend = await db.friends.get(1);
    console.log(firstFriend);
}
Clone this wiki locally