Search code examples
functional-programmingerlangrebar3

Cannot find function `jsx:is_json/1` although jsx is included and compiled


I am writing a webapp using Erlang toolchain (OTP, rebar3, cowboy, jsx...). The following code does not work because jsx:is_json/1 cannot be found during runtime.

handle_login(Req, State) ->
  {ok, Data, _} = cowboy_req:body(Req),

  case jsx:is_json(Data) of
    false -> cowboy_req:reply(400,
      [
        {<<"content-type">>, <<"application/json">>}
      ],
      <<"Invalid JSON">>,
      Req);

Stacktrace:

{[{reason,undef},
   {mfa,{erbid_api_handler,handle,2}},
   {stacktrace,
      [{jsx,is_json,[<<"{\"username\":\"tom\"}">>],[]},
       {erbid_api_handler,handle_login,2,
           [{file,
                "/Users/khanhhua/dev/project-erbid/_build/default/lib/erbid/src/erbid_api_handler.erl"},
            {line,45}]},
       {erbid_api_handler,handle,2,
... truncated for brevity

Folder structure: Project's _build folder structure

I need to know how to fix the issue. Thanks.


Solution

  • I have found the cause of this issue. I did not not include module jsx in my erbid.app.src's applications section.

    {application, erbid, [
        {description, "Realtime system"},
        {vsn, "0.1.0"},
        {registered, []},
        {applications, [
            kernel,
            stdlib,
            cowboy,
            jsx
        ]},
        {mod, {erbid, []}},
        {env, []}
    ]}.
    

    Totally due to my lack of Erlang experience.