Search code examples
erlangmnesia

Erlang check empty string


I'm new in Erlang development. I'm trying to read value of AccessToken from mnesia table (stored in 'users' table).

In my code I've done:

BUser = boss_db:find(users, [{email, 'equals', MyEmail}]),
[{_,_,BEmail,BName,BPassword,_,BAccessToken}] = BUser,
io:format("User Details ~n~p~n", [BUser]),
io:format("Access Token ~n~p~n", [BAccessToken]),
.
.

I get:

User Details 
[{users,"users-1","mymail@someemail.org","Some Name","somepassword",'',''}]

The Last field is AccessToken and it is printed as:

Access Token 
''

If AccessToken value is non empty I'll do some operation.

How do I check if AccessToken is empty or not?

I tried:

AccessTokenLength = length(BAccessToken)
if AccessTokenLength > 0 ->
.
.

But I get the following error:

Error in controller error badarg [{erlang,length,[''],[]}

I also tried:

case binary_to_list(BAccessToken) =/= [] of
true->
false->

But I get the following error:

Error in controller error badarg [{erlang,binary_to_list,[''],[]}

How do I check the empty condition properly?


Solution

  • You could just pattern match on value

    case BAccessToken of
         '' ->
             %% empty 
         _ ->
             %% not empty
    end
    

    And one more thing. Single quotes in Erlang signifies atom. Usually you can write those with starting lover case letter, like atom or false or not_empty. But sometimes you would like to use some more "complex" atom you could use single quote like 'This is also atom'. And '' is just "empty atom.

    And empty binary would look like this <<>>.