firstWhere()

Get the first matching accessible document in the collection.

Signature

firstWhere(predicate: () => Boolean)): <Collection> | Null

firstWhere(predicate: (val: Document) => Boolean)): <Collection> | Null

Description

Gets the first accessible document in the collection that matches the predicate function.

Parameters

Parameter Type Required Description

predicate

Function

Anonymous function that compares values in each document in the collection and returns true for matches or false for mismatches.

predicate parameters:

Parameter Type Required Description

val

Document

Collection document to compare. If omitted, use dot notation to access document properties.

Return value

One of:

Type Description

Document

First accessible document matching the predicate function.

Null

When no document matches the predicate function or all matching documents are inaccessible.

Examples

Product.firstWhere(.quantity < 20)
{
  id: "394786008170758210",
  coll: Product,
  ts: Time("2099-04-10T13:35:26.547Z"),
  name: "pinata",
  description: "Giant Taco Pinata",
  price: 23.99,
  quantity: 0,
  store: Store("394786008147689538"),
  backorderLimit: 10,
  backordered: true
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!