If

If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
if_expr( cond_expr, true_expr, false_expr )
if_( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )
If( cond_expr, true_expr, false_expr )

Description

The If function evaluates and returns true_expr or false_expr depending on the value of the "cond" expression. If the cond expression evaluates to anything other than a Boolean, If returns an "invalid argument" error.

Parameters

Argument Type Definition and Requirements

cond

Boolean Expression

The conditional expression to evaluate as true or false.

true_expr

Expression

The FQL statement or variable to return if cond is true.

false_expr

Expression

The FQL statement or variable to return if cond is false.

Returns

The evaluation of either true_expr or false_expr.

Examples

The query below evaluates the first expression, the conditional expression, and then determines that the conditional expression returns true. This causes the second argument, the true expression, to be evaluated and returned. The third argument, the false expression, is never evaluated.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{ "if": true, "then": "was true", "else": "was false" }'
client.Query(If(true, "was true", "was false"));
System.out.println(
         client.query(
                 If(Value(true), Value("was true"), Value("was false"))
         ).get() );
result, _ := client.Query(f.If(true, "was true", "was false"))

fmt.Println(result)
client.query(If(true, "was true", "was false"))
client.query(q.if_expr(True, "was true", "was false"))
$client.query do
  if_(true, 'was true', 'was false')
end
client.query(If(true, then: "was true", else: "was false"))
client.query(
  q.If(true, 'was true', 'was false')
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{ "resource": "was true" }
"was true"
"was true"
was true
"was true"
"was true"
"was true"
"was true"
"was true"

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!