Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix 1.3 (Elixir) Error: Myapp.Users.User.__struct__/0 is undefined, cannot expand struct Myapp.Users.User

Trying to build an API using Phoenix v1.3 following this tutorial:
https://dreamconception.com/tech/phoenix-full-fledged-api-in-five-minutes
(made sure to follow each step to the letter)

When I attempt to run mix ecto.setup

I get the following error: error

Compiling 17 files (.ex)

== Compilation error on file lib/myapp/users/users.ex ==
** (CompileError) lib/myapp/users/users.ex:65: Myapp.Users.User.__struct__/0 is undefined, cannot expand struct Myapp.Users.User
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    lib/myapp/users/users.ex:65: (module)
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

The lib/myapp/users/users.ex is quite big: https://github.com/nelsonic/phoenix-api-example/blob/master/lib/myapp/users/users.ex#L65

Complete code on GitHub: https://github.com/nelsonic/phoenix-api-example

Any help debugging it would be much appreciated.

like image 617
nelsonic Avatar asked Jun 06 '17 21:06

nelsonic


1 Answers

So there are a few things that I'm noticing:

  1. You've named your context after your record. You should name your context after the idea the records are a part of. Something like Authentication or Account

  2. Building off of #1, in a Phoenix 1.3 app where you have an Account context, you might have user records associated with that context. In that case you'd have Myapp.Account.Users defined in lib/myapp/account/users.ex. This is where your struct would come from.

  3. I think how you've ended up here, is that the context is named the same as your records and that's causing some confusion. On line 9 of your Users context you're aliasing Myapp.Users.User but that module is never defined.

like image 54
Tyler Willingham Avatar answered Dec 09 '22 20:12

Tyler Willingham