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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

We're in Beta

About us Mission Statement