Function()
Learn: User-defined functions (UDFs) |
---|
Call a user-defined function (UDF) by its name.
Description
Errors
If you attempt to call a UDF that doesn’t exist, Fauna returns a query
runtime error with an invalid_argument
error code and a 400 HTTP status
code:
invalid_argument
error: invalid argument `function`: No such user function `Foo`.
at *query*:1:9
|
1 | Function("Foo")
| ^^^^^^^
|
Comparison to <functionName>
Calling Function()
is similar to accessing <functionName>
directly,
except Function()
returns an Any value.
This difference only affects static typing, not runtime behavior.
In most cases, you should use <functionName>
. However, Function()
is
useful if you need to iterate through a list of UDF calls.
Examples
Get the function by passing the function name to Function()
:
// Call function named `getOrCreateCart`
Function("getOrCreateCart")
"[function getOrCreateCart]"
To call the function, pass arguments in parentheses:
// Calls the `getOrCreateCart()` function on
// a `Customer` document with an `id` of `111`.
Function("getOrCreateCart")('111')
{
id: "412998994633949261",
coll: Order,
ts: Time("2024-10-28T14:23:03.966Z"),
items: "hdW...",
total: 5392,
status: "cart",
customer: Customer("111"),
createdAt: Time("2024-10-28T14:23:03.795981Z"),
payment: {}
}