key.exists()

Learn: Keys

Test if a key exists.

Signature

exists() => Boolean

Description

Tests if an key, represented as an Key document, exists.

A key is a type of authentication secret used for anonymous access to a Fauna database. Unlike tokens, keys are not associated with an identity.

exists() vs. null comparisons

You can use either exists() or a null comparison (== null or != null) to check the existence or validity of a value. For example:

Key.byId("12345").exists()  // true
Key.byId("12345") != null   // true

Key differences:

  • exists() returns an error if called on an unsupported value.

  • Null comparisons do not throw errors and work safely on any value.

For example:

// Declare an object. Objects don't support
// an `exists()` method.
let object = { a: "Foo", b: "Bar" }

object.exists()  // Returns `invalid_query` error

object != null   // Returns true

Parameters

None

Return value

Type Description

Boolean

If true, the Key document exists. If false, the Key document doesn’t exist.

Examples

Key.byId("412655134325080576").exists()
true
\