next.jsnosqlfaunadb

Faunadb conditional statement can't run false condition expression


I tried to build fulltext search API using nextjs and faunadb. I referenced the article "How to Get a Full-Text Search with FaunaDB" My index setting is as below. enter image description here This API will receive two values artist and term(short for search term)
If there is two, it will combined by and statement.
Or it will run there own statement or each value.
Implemented code is as below.

const queryCase = req.body.artist && req.body.term ? 1 : req.body.term != undefined ? 2 : 3

const tokens = await serverClient.query(
   Filter(
      Paginate(Match(Index("all_token_by_artist_and_description"), true)),
      Lambda(
         [
            "artist",
            "description",
            "image",
            "seller",
            "creator",
            "category",
            "refId"
         ],
         If(
            Equals(queryCase, 1),
            And(
               Or(
                  ContainsStr(
                     LowerCase(Var("artist")), 
                     req.body.term
                  ),
                  ContainsStr(
                     LowerCase(Var("description")), 
                     req.body.term
                  )
               ),
               Equals(
                  LowerCase(Var('artist')), 
                  LowerCase(req.body.artist)
               )
            ),
            If(
               Equals(queryCase, 2),
               Or(
                  ContainsStr(
                     LowerCase(Var("artist")), 
                     req.body.term
                  ),
                  ContainsStr(
                     LowerCase(Var("description")), 
                     req.body.term
                  )
               ),
               Equals(
                  LowerCase(Var('artist')), 
                  LowerCase(req.body.artist)
               )
            )
         )
      )
   )
)

res.status(200).json(tokens.data)

It couldn't run false condition expression. The false expression works fine when I switched truth expression and false expression.
I get it fixed by make 3 separate query.

if(queryCase == 1){
   tokens = await serverClient.query(
              .....
   )
}else if(queryCase == 2){
   tokens = await serverClient.query(
              .....   
   )             
}else{
   tokens = await serverClient.query(
              .....
   )                
}

But I really have no idea what's wrong with the original query. Can anyone tell me the problem with the query?


Solution

  • It caused by missing value. So I assigned random string to the missing value after the case is determined. eg) If I only get "artist" value from request, assign random string to the "term".