简体   繁体   English

在 Angular 中发布 http://localhost:3000/auth 400(错误请求)

[英]POST http://localhost:3000/auth 400 (Bad Request) in Angular

I'm trying to send register form data to my server, but receive (Bad Request).我正在尝试将注册表单数据发送到我的服务器,但收到(错误请求)。 I test my server with postman & it work correctly with this data:我用 postman 测试了我的服务器,它可以正常使用以下数据:

{
    "username": "root",
    "email": "root@gmail.com",
    "password": "Pesho@"
}

But have problem with Angular & can't find were is the problem.但是 Angular 有问题 & 找不到是问题所在。

This is register.component.ts:这是register.component.ts:

export class RegisterComponent implements OnInit {
  public registerFormGroup: FormGroup;

  constructor(
    private readonly auth: AuthService,
  ) { }

  ngOnInit() {
    this.registerFormGroup = this.auth.registerFormGroup(this.registerFormGroup);
  }

  public register(username: string, email: string, password: string) {
    this.auth.register(username, email, password);
    console.log({username});
    console.log({email});
    console.log({password});
  }
}

This is auth.srvice.ts:这是auth.srvice.ts:

public register(username: string, password: string, email: string): Subscription {
    return this.http.post('http://localhost:3000/auth',
      {
        username,
        email,
        password,
      })
      .subscribe(
        (success: any) => {
          this.notificator.success(success.message);
          setTimeout(() => {
            this.login(email, password);
          }, 1500);
        }
        ,
        (err) => {
          this.notificator.error(err.error.message);
        }
      );
  }

And this is the small html register form - register.component.html:这就是小html寄存器形式——register.component.html

<form class="example-form" [formGroup]="this.registerFormGroup"
      (ngSubmit)="this.register(name.value, email.value, password.value)" autocomplete="off">

      <mat-card-content>

        <mat-form-field class="example-full-width">
          <input matInput #name autocomplete="off" formControlName="name" type="text" class="form-control"
          placeholder="Name *">
        </mat-form-field>

        <mat-form-field class="example-full-width">
          <input matInput #email autocomplete="off" #email matInput class="form-control" placeholder="Email *" type="email"
          formControlName="email">
        </mat-form-field>

        <mat-form-field class="example-full-width">
          <input matInput #password formControlName="password" type="password" class="form-control" placeholder="Password *">
        </mat-form-field>

      </mat-card-content>

      <button mat-stroked-button color="accent" class="btn-block" type="submit" value="Sign Up">Register</button>

    </form>

I put few console logs to see what the form sends & they are looking correct:我放了一些控制台日志来查看表单发送的内容以及它们看起来是否正确:

{username: "root"} username: "root" proto : Object {用户名:“root”} 用户名:“root”原型:Object

{email: "root@gmail.com"} email: "root@gmail.com" proto : Object {电子邮件:“root@gmail.com”} email:“root@gmail.com”原型:Object

{password: "Pesho@"} password: "Pesho@" proto : Object {密码:“Pesho@”} 密码:“Pesho@”原型:Object

I used the same logic in previous project with Bootstrap & it worked perfect.我在之前的 Bootstrap 项目中使用了相同的逻辑,并且效果很好。 In this case use Material, but I don't think the problem is from there.在这种情况下使用材料,但我认为问题不在于那里。 I didn't receive any errors in NestJS server terminal.我在 NestJS 服务器终端中没有收到任何错误。

Problem is the order in which you are passing your arguments问题是您传递 arguments 的顺序

This is your register.component.ts:这是你的register.component.ts:

public register(username: string, email: string, password: string) {
this.auth.register(username, email, password); <---- Here you are passing username,email,password
console.log({username});
console.log({email});
console.log({password});
  }
}

This is auth.srvice.ts: Here you are accepting username,password,email这是auth.srvice.ts:在这里您接受用户名、密码、email

public register(username: string, password: string, email: string): Subscription {
return this.http.post('http://localhost:3000/auth',
  {
    username,
    email,
    password,
  })

So to fix this switch arguments in either register.component.ts or auth.srvice.ts因此,要在register.component.tsauth.srvice.ts 中修复此开关 arguments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM