Collection.each()

Syntax

collection.each(callback)

Parameters

callback: Function function (item, cursor) { }

Callback Parameters

item: Object Found object
cursor: IDBCursor The cursor of the object being iterated.

Return Value

Promise

Remarks

Iterate through all objects in the collection in an implicit single readonly transaction. If you need to modify or delete items from within the callback, you could use Collection.modify() in place of Collection.each(), since it will use a readwrite transaction rather than a readonly transaction. You could also explicitly surround your call in a READWRITE transaction.

When iteration finishes, the returned Promise will resolve with undefined, calling any Promise.then() callback.

If the operation fails, the returned Promise will be rejected, calling any Promise.catch() callback.

NOTES:

  • The operation will implicitly be called from within a READONLY transaction unless you already surround your code with a transaction.
  • The callback should not modify the database. If that is required, use Collection.modify() instead.
  • The return value from your callback is ignored, so returning a Promise from it will have no effect.
  • In many cases, it is better and more optimized to use any of the following methods when reading from a Table or Collection:

Sample

const db = new Dexie('dbname');

db.version(1).stores({
  friends: `
    id,
    name,
    age`
});

// Populate table
db.friends.bulkPut([
  {id: 1, name: "Foo", age: 33},
  {id: 2, name: "Bar", age: 44},
  {id: 3, name: "Someone", age: 1}
]).then(() => {
  // Iterate all friends, ordered by id:
  console.log("All my friends, ordered by id:");
  return db.friends.each(friend => console.log(friend.name));
}).then(() => {
  // Iterate all friends, ordered by age:
  console.log("All my friends, ordered by age:");
  return db.friends.orderBy('age').each(friend => console.log(friend.name));
}).then(() => {
  // Iterate all friends where age is above 30.
  console.log("Friends over 30 years old:");
  return db.friends.where('age').above(30).each(friend => console.log(friend.name));
}).catch (err => {
  console.error (err);
});

Table of Contents