I have some code that get a list of file from a directory and I want to send it back as a json to the client
getDir('GET', [])->
{ok,Sheets} = files(FileDir),
Sheets2 = sheetJson(Sheets, ""),
{json, [{sheets, Sheets2}]}.
sheetJson([H|T], [])->
{File, Name, Size} = H,
Str = [{file, File},{name, Name}],
sheetJson(T, Str);
sheetJson([H|T], Str)->
{File, Name, Size} = H,
Acc = Str++[{file, File},{name, Name}],
sheetJson(T, Acc);
sheetJson(_, Str)->Str.
this code will return
{"sheets":{"file":"0-Jason .csv","name":"Jason ","file":"1-State.csv","name":"State","file":"2-country.csv","name":"country"}}
But was wanting something more like
{"sheets":{"file":"0-Jason .csv","name":"Jason "},{"file":"1-State.csv","name":"State"},{"file":"2-country.csv","name":"country"}}
and if I try changing the code to anything then that I get error messages like
Unhandled Error: error:function_clause. Stacktrace: [{boss_json,json_data1,[[{sheet,{file,"0-Sheet1.csv"},{name,"Sheet1"}}],[],[]],[{file,"src/boss/boss_json.erl"},{line,31}]},{boss_json,json_data1,3,[{file,"src/boss/boss_json.erl"},{line,42}]},{boss_json,encode,2,[{file,"src/boss/boss_json.erl"},{line,16}]},{boss_web_controller_render,process_action_result,4,[{file,"src/boss/boss_web_controller_render.erl"},{line,171}]},{boss_web_controller,execute_action_inner,9,[{file,"src/boss/boss_web_controller.erl"},{line,337}]},{boss_web_controller_handle_request,process_dynamic_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,242}]},{boss_web_controller_handle_request,process_request,4,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,228}]},{boss_web_controller_handle_request,set_timer,7,[{file,"src/boss/boss_web_controller_handle_request.erl"},{line,148}]}]
Edit : Solution I came up with
I just ended up just writing a json String and using {output, _} instead of {json, _}, the benefit was I could see the Json string in the javascript error and fix, where as with erlang I just get a massive message that tells me nothing.