h1d3r00t

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

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

Aetherus

Maybe you should try iex -S mix instead of just iex.

Last Post!

h1d3r00t

h1d3r00t

Thank you. I solved this problem.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New