odyright

odyright

Angular5 POST request to Phoenix Frramework (api), send 422 (Unprocessable Entity)

I was running Angular 5 and Phoenix framework and I’m trying to send a POST request to Phoenix. i got 422 Unprocessable error
enter image description here

I have the following code:

enrollment.component.ts

saveEnrollment()
{
    this.enrservice.saveEnrollment(this.enrollment)
        .then(
            (enrollment: Enrollment) => {
                // Show the success message
                this.snackBar.open('Enrollment success', 'OK', {
                    verticalPosition: 'top',
                    duration        : 6000
                });
          
            });
}

enrollment.component.ts

saveEnrollment(enrollment: Enrollment)
{
    return new Promise((resolve, reject) => {
        this.http.post(API_URL, enrollment)
            .subscribe((response: any) => {
                resolve(response);
            }, reject);
    });
}

enrollment.model.ts

export class Enrollment {
id: number;
firstName: string;
lastName: string;
address: string;
city: string;
state: string;
postalCode: string;

constructor(enrollment?)
{
    enrollment = enrollment || {};
    /* this.id = enrollment.id || FuseUtils.generateGUID(); */
    this.id = enrollment.id || '';
    this.firstName = enrollment.firstName || '';
    this.lastName = enrollment.lastName || '';
    this.address = enrollment.address || '';
    this.city = enrollment.city || '';
    this.state = enrollment.state || '';
    this.postalCode = enrollment.postalCode || '';
}}

and when i try to submit the form..i got that result:

[info] POST /api/enrollments
[debug] Processing with UmappWeb.EnrollmentController.create/2
Parameters: %{"address" => "", "city" => "", "firstName" => "", "id" => "", 
"lastName" => "", "postalCode" => "", "state" => ""}
 Pipelines: [:api]
[info] Sent 422 in 0┬Ás

Not sure what I’m doing wrong. please i need advise or help! thank you

Marked As Solved

odyright

odyright

everything works now… it was due to a mismatched TYPO… on server side Repo migration in the database. that’s why i got 422 (unprocessable entity)

Thank you @all of you.

Also Liked

odyright

odyright

Harrrrr, it seems the problem is the angular’s part..when i investigated the request payload part shown blank data for each input sent to the server(phoenix framework) but i don’t know where i’m wrong ///// :tired_face::tired_face:

odyright

odyright

anyone .. have an idea?

idi527

idi527

You would need to provide more information, probably. Or try to trace this enrollment object up to its creation, and see why it is empty.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44608 311
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

We're in Beta

About us Mission Statement