8
votes

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.

1
You don't seem to have a module of MyApp.Users.User.Justin Wood
Shouldn't those files under web/ be in lib/ in Phoenix 1.3? I can see defmodule Myapp.Users.User in web/models/coherence/user.ex.Dogbert
@Dogbert well spotted, I think Coherence is assuming it's a V1.2 app...nelsonic

1 Answers

3
votes

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.