Table.get()
Syntax
table.get(primaryKey): Promise
table.get({keyPath1: value1, keyPath2: value2, ...}): Promise
Parameters
| primaryKey | Primary key of object to get | |
| {keyPath1: value1, keyPath2: value2, ...} | Criteria to filter |
Callback Parameters
| item: Object | Found item if any, otherwise undefined. |
Return Value
If no item was found, then the returned promise will resolve with
undefined. Otherwise it will resolve with the found value.Remarks
Fetches an object with a given primaryKey or where the given criteria
({keyPath1: value1, keyPath2: value2}) are fulfilled and returns the first matching result.If the operation succeeds, then the returned Promise will resolve with the result of the operation, calling any Promise.then() callback.
If the operation fails, then the returned promise will reject, calling any Promise.catch() callback.
Samples
/* This code gets an object by its primary key:
*/
const firstFriend = await db.friends.get(1);
alert ("Friend with id 1: " + firstFriend.name);
/** This function queries a friend by indices firstName and lastName. It also resolves some
relational data in the same result.
*/
function getAustinWithVehicles() {
return db.transaction('r', [db.friends, db.vehicles], async () => {
const austin = await db.friends.get({firstName: "Austin", lastName: "Powers"});
// Query by "foreign key" on vehicles:
const austinsVehicles = await db.vehicles.where({owner: austin.id}).toArray();
// Include the vehicles in the result:
austin.vehicles = austinsVehicles;
return austin;
});
}