h1d3r00t
I cannot import(alias) defstruct
I’m learning Pheonix via programming Pheonix > 1.4 book. The chapter 3 code cannot run.
defmodule Rumbl.Accounts.User do
defstruct [:id, :name, :username]
end
defmodule Rumbl.Accounts do
alias Rumbl.Accounts.User
def list_users do
[
%User{id: "1", name: "jose", username: "josevalim"}
]
end
end
Then run in iex. I faced this error.
** (CompileError) accounts.ex:7: Rumbl.Accounts.User.__struct__/1 is undefined, cannot expand struct Rumbl.Accounts.User. Make sure the struct name is correct. If the struct name exists and is correct
but it still cannot be found, you likely have cyclic module usage in your code
What am I wrong?
Marked As Solved
Aetherus
Sorry for being late to reply. I’m in China so I was sleeping when you asked the question.
iex -S mix starts an iex session with all the .ex files in the elixirc paths compiled and loaded (including the .ex files in the dependencies of your project), all the “applications” of your project started, and all the supervision trees built. But, as for a phoenix project, iex -S mix does not start the web server.
mix phx.server also compiles all the .ex files, starts, all the applications, and builds all the supervision trees. It also starts the web server, but it does not start an iex session.
If you want both, you can try iex -S mix phx.server.
There is no iex -S your_module.ex. You can start an mix-aware iex session by iex -S mix, and in that session, do whatever you want with your modules. Or, you can type mix eval '<your code>'.
Also Liked
Aetherus
Maybe you should try iex -S mix instead of just iex.









