王爱国
王爱国

Reputation: 1

elixir/phoenix ecto how to judge return condition?

Given the code

def getbno() do
  query = from u in GodowentryForAcceptance,
    select: max(u.bno)
  Repo.one(query)
end

I want to judge return condition such as

case GodownentryForAcceptanceService.getbno() do
  empty-> do something
  notempty -> dosomething
end

What is judge condition(empty/not empty)?

Upvotes: 0

Views: 414

Answers (1)

bla
bla

Reputation: 1870

Acording to the docs, Repo.one/2 returns nil if no result was found. So in order to check for it you could do something like this (as @Dogbert mentioned):

case GodownentryForAcceptanceService.getbno() do
  nil -> do_something() # empty
  value -> dosomething(value) # not empty
end

Also note that, if more than one result is found in the query, Repo.one/2 raises an error (Ecto.MultipleResultsError).

Upvotes: 1

Related Questions