wktdev

wktdev

Can someone please help me with this (Basic API creation) Rubber Ducky Edition

I am using Phoenix1.17 and the respective versions of Elixir and Erlang. I used ASDF to set it up on Ubuntu

I created a basic Phoenix Instance

mix phx.new app

I install all the dependencies. When the installer gets to the page asking me to run mix ecto.create
I stop what I am doing.

At his point I create a new command line instance, go into PostGres and I create a database. I name the database app.

I verify the existence of the database in Postgres using the command: \l

I go back to to the command line instance that I am running the phoenix install from. I CD into the app directory. I run mix ecto.create

I get a validation message

Compiling 15 files (.ex)
Generated app app
The database for App.Repo has been created

I go to the phoenix documentation to look up how to generate routes. The Phoenix documentation doesn’t say the right way to do it directly; instead, it demonstrates it in the context of building an app. I don’t want to build the app in the documentation, I simply want to know the correct syntax and read a few assuring words explaining it.

Based on what they have this is the synax they use:

mix phx.gen.json Urls Url urls link:string title:string

I am going to replace Urls Url urls with Items Item items and I am only going to have one table called name:string

mix phx.gen.json Items Item items name:string

I get the prompt


Add the resource to your :api scope in lib/app_web/router.ex:
    resources "/items", ItemController, except: [:new, :edit]

I do what it says. I go to router and put that puppy in there:

  pipeline :api do
    plug :accepts, ["json"]
    resources "/items", ItemController, except: [:new, :edit]
  end

I run the following command:

mix ecto.migrate

Welp, I have a warning. I have no idea what it means or why it appears.


Compiling 7 files (.ex)
warning: no route path for AppWeb.Router matches "/api/items/#{item}"
  lib/app_web/controllers/item_controller.ex:18: AppWeb.ItemController.create/2

warning: ItemController.init/1 is undefined (module ItemController is not available or is yet to be defined)
  lib/app_web/router.ex:2: AppWeb.Router.__checks__/0

Generated app app

21:16:43.543 [info] == Running 20230205031130 App.Repo.Migrations.CreateItems.change/0 forward

21:16:43.547 [info] create table items

Can someone explain how the hell I’m supposed to read this? There is no hint as to how to fix this

Ignoring the warning, the next thing I want to do is determine if my app configured correctly. I honestly have no idea how to do that other than to run it and assume that if I go to 4000/items that I will see an empty array or JSON object.

mix phx.server

I go to localhost:4000/items

A missing route.

I try localhost:4000/api/items

A missing route.

I looked at the Phoenix instructions and it says to run a curl command. I did.

curl -i http://localhost:4000/api/items

I got this:


HTTP/1.1 404 Not Found
cache-control: max-age=0, private, must-revalidate
content-length: 56354
content-type: text/html; charset=utf-8
date: Sun, 05 Feb 2023 03:24:09 GMT
server: Cowboy

What am I doing wrong?

EDIT

I thought I shut down the server and ran it again. The error below ran.

(Mix) Could not start application app: App.Application.start(:normal, []) returned an error: shutdown: failed to start child: AppWeb.Endpoint
    ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, AppWeb.Endpoint.HTTP}
        ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
            ** (EXIT) {:listen_error, AppWeb.Endpoint.HTTP, :eaddrinuse}

The error has no hint that another instance of the server is running. Shouldn’t it tell the operator that “You may have another instance running on this port”. Most other platforms will tell you that.

Most Liked

jhefreyzz

jhefreyzz

You put resources "/items", ItemController, except: [:new, :edit] inside a scope block as mentioned above.

In your case it should be like this:

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

 scope "/", AppWeb do
   pipe_through :api
   
   resources "/items", ItemController, except: [:new, :edit]
 end

To have an idea of what routes being generated of the resources then type mix phx.routes in your terminal.

wktdev

wktdev

Thanks this worked. I appreciate it. The prompt instructions in the command line should be more clear and beginner friendly. The documentation is difficult.

Add the resource to your :api scope in lib/app_web/router.ex:
    resources "/items", ItemController, except: [:new, :edit]

I assumed they wanted me to put the code in the function with the :api atom.
They should have an example.

EDIT:
The documentation does have an example but I was reading off the command prompt messages. So for anyone reading this, the command prompt can be confusing.

scope "/api", LinksWeb do
  pipe_through :api
  resources "/urls", UrlController, except: [:new, :edit]
end

Where Next?

Popular in Questions Top

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement