How to use application:get_env() in Erlang/OTP?

浪尽此生 提交于 2019-12-24 01:08:32

问题


I created a mochiweb instance

src/
   |-- Makefile
   |-- room.erl
   |-- myserver.app
   |-- myserver.erl
   |-- myserver_app.erl
   |-- myserver_deps.erl
   |-- myserver_sup.erl
   |-- myserver_web.erl
   `-- uuid.erl

in myserver_web.erl I am able to access the application config

{ok, "0.0.1"} = application:get_key(vsn),

However in room.erl, I am not able to access the application config (specifically the env list).

undefined = application:get_key(vsn),

The supervisor does not start the room, nor do I want it too.

I'm new to OTP and I realize I'm probably doing something stupid, but I would really appreciate anyone's help.

Cheers!


回答1:


From the Erlang documentation about the application:get_key/1-2 function:

Returns the value of the application specification key Key for Application. If the application argument is omitted, it defaults to the application of the calling process.

What's the application of my_server_web.erl? What's the application of room.erl?

If the specified application is not loaded, or the specification key does not exist, or if the process executing the call does not belong to any application, the function returns undefined.

Is the key specified? Is the application loaded?

Regarding the "env" key, you can use the application:get_env/1 function instead.




回答2:


I figured it out... The answer is quite stupid and I feel silly posting, but I hope someone else can benefit from me.

I was actually doing everything right (as are all the other answers), but I was using mochiweb's auto reloader and not actually stopping the server. Once I did that everything was fine.

DOH! Sorry!




回答3:


Use get_key(myserver, vsn) instead. Note that the application needs to be loaded first.



来源:https://stackoverflow.com/questions/4112339/how-to-use-applicationget-env-in-erlang-otp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!