Function PhxReactList7Web.BlogsController.init/1 is undefined (module PhxReactList7Web.BlogsController is not available)**

Hello,

I working on an http api app creating the controllers/router info manually and I am getting this error:

**function PhxReactList7Web.BlogsController.init/1 is undefined (module PhxReactList7Web.BlogsController is not available)**

here are the files in play:

blogs_controller.ex

defmodule PhxReactList7Web.BlogController do
  use PhxReactList7Web, :controller

  alias PhxReactList7.Blogs
  alias PhxReactList7.Blogs.Blog

  def index(conn, _params) do
    blogs = Repo.all(Blogs)
  end

end

router.ex

defmodule PhxReactList7Web.Router do
  use PhxReactList7Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", PhxReactList7Web do
    pipe_through :browser
    get "/", PageController, :index
    get  "/blogs",     BlogsController, :index  # show all blog posts
  end

  if Mix.env() in [:dev, :test] do
    import Phoenix.LiveDashboard.Router

    scope "/" do
      pipe_through :browser
      live_dashboard "/dashboard", metrics: PhxReactList7Web.Telemetry
    end
  end
end

elixir v.1.11.3
phoenix: v1.5.7
postgresql: v13
node: v14
windows: 10

after searching the web for an answer and coming up empty, I posted the question. the lab I am working from is using Phoenix v1.3 and Elixir 1.6. The lab uses the database model feature and the folder structure is somewhat different than the one used now.

Phoenix, HTTP, controllers, router

You have a typo BlogController Vs BlogsController

1 Like

thank you @tcoopman sharp eye, now I have the following error…

function Repo.all/1 is undefined (module Repo is not available)

thanks again, I hope to stop making these rookie mistakes…

I think I found the answer: I need to use an alias like: alias PhxReactList7Web.Repo

will try that and work on the rest of the errors that are going to come up…

Thanks again.

1 Like

It’s more likely to be alias PhxReactList7.Repo (without the Web part).

If you are confused, just do something like this (assuming you have ripgrep installed, which you should!):

rg 'defmodule.+\.Repo '

And it will give you a list of files that match. You can then quickly check the name of the module right there in the search results.

That, or use an IDE. :slight_smile:

2 Likes

The find command is quite powerful too.

$ find lib/ -type f -exec grep -n -H "defmodule.*Repo" {} \;
1 Like

Well, ripgrep does both together. :smiley: That’s why I prefer it. Plus it’s using all CPU cores unlike the GNU tools.

Also don’t forget that Phoenix actions need to return a conn your current function returns an :ok (the default value when there is none). So you might need to do something like:

def index(conn, _params) do
 blogs = Repo.all(Blog)
 render(conn, "index.json", blogs: blogs)
end

More info on rendering JSON you can find here: Rendering JSON

About the render function: Phoenix.Controller.render/3

1 Like

thank you much, your suggestion is still beyond my understanding, but will explore it…

i’m on windows system so the find works a little different

@ joaoevangelista
thanks for the tip. I’m working with the labs from a 1917 article that uses phx 1.3 which is different to the 1.5.7 version that I’m using. I guess I could download the 1.3 version but I want to learn to the current version which is what is being used in my new job starting 2 weeks from now.

I will explore your suggested changes to see if they work. the problem I’m also having is that every time I make a change to the code I end up getting a ton of other error messages like:

protocol Ecto.Queryable not implemented for PhxReactList7.Blogs of type Atom, the given module does not provide a schema. This protocol is implemented for the following type(s): Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple

so I’m still catching my breath…

thanks again… this really helps… I’m slow, but getting there…

@joaoevangelista

using you code change I now get the following error message:

function PhxReactList7Web.BlogsView.render/2 is undefined (module PhxReactList7Web.BlogsView is not available)

I’m going to start the project from scratch and go from there, this project is now so broken the error messages get more confusing by the second. no… nano second…
anywho: Obrigado

Thanks @dimitarvp

I’m on a windows machine and don’t have the linux tool set installed, that’s going to be my next task…

LC

Maybe you should develop under WSL2? Much easier this way. And it’s basically Linux inside Windows.

it’s because now it returns correctly but there is no view module. You can check about it here. What you can do to easy your learning is to create a new Phoenix project and scaffold all the pieces. Then you can change code all around to see how things are setup.

The guides section of the documentation also helps a lot with understanding what is needed for Phoenix

Versions in the 1.0 major version does not break anything, just deprecate.

For now there is no need to change from Windows, you can use your editor “search everywhere” feature to quickly find.