forEach()

Iterate over all values in a Set, executing a function on each value.

Signature

forEach(functionBody: (value: T) => any): null

Description

The forEach() method iterates over all values in the Set and executes the provided functionBody on each value. It is used for mutations or writing documents based on each Set value.

Because this method scans the full set, it returns an error if there are more than 16000 documents in the set. This method can timeout for large sets under that limit.

Iterator methods

FQL provides several methods for iterating over a set. forEach() and map() are similar but used for different purposes.

To perform writes on a set’s elements, use forEach(). forEach() doesn’t return a value.

To return a new set containing projected or transformed elements, use map(). map() can’t perform writes.

flatMap() works like map() except it flattens the resulting set by one level.

Parameters

Parameter Type Required Description

functionBody

Function

Yes

Anonymous Function that operates on a Set value and returns nothing.

functionBody parameters:

Parameter Type Required Description

value

Any

Yes

Value to process.

Return value

None

Examples

Update all Customers documents where the first name is Carol and the last name is Clark, to change the phone number to 1-555-555-1212:

Customer
  .all()
  .where(.firstName == 'Carol' && .lastName == 'Clark')
  .forEach(
    customer => customer.update({ telephone: '1-555-555-1212' })
  )
null

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!