javascript - 我无法使用 FormGroup Angular 6 发出发布请求

标签 javascript html angular forms typescript

我是 Angular 新手,无法使用 formGroup 发出发布请求。正在为我拥有的所有字段发送正文,但是由于以下错误,出现错误 400(错误请求):{"status":"error","errors":{"message":"A secondary在上一个操作完成之前在此上下文上开始操作。这通常是由于不同的线程使用 DbContext 的同一实例引起的,但是不能保证实例成员是线程安全的。这也可能是由于在客户端上评估嵌套查询引起的,如果是这种情况,请重写查询,避免嵌套调用。"}},我有点明白错误是什么,但我不明白我的代码出了什么问题。提前致谢

注册.model.ts

export class User{
    FirstName: string;
    LastName: string;
    UserName: string;
    Email: string;
    Password: string;
}

注册.service.ts

RegisterUser(user: User){
return this.http.post('url', user);

}

注册.component.html

<form [formGroup]="registerForm" (ngSubmit)="onSubmit()">

  <div class="form-group row">
    <label class="col-sm-3 col-form-label">First Name:</label>
    <div class="col-sm-3">
      <input type="text" formControlName="FirstName" class="form-control"
        placeholder="First Name" ngModel>
    </div>
  </div>
  <div class="form-group row">
    <label class="col-sm-3 col-form-label">Last Name:</label>
    <div class="col-sm-3">
      <input type="text" formControlName="LastName" class="form-control"
        id="LastName" placeholder="LastName" ngModel>
    </div>
  </div>
  <div class="form-group row">
    <label for="UserName" class="col-sm-3 col-form-label">Username:</label>
    <div class="col-sm-3">
      <input type="text" formControlName="UserName" class="form-control"
        placeholder="User Name" ngModel>
    </div>
  </div>
  <div class="form-group row">
    <label for="Email" class="col-sm-3 col-form-label">Email:</label>
    <div class="col-sm-3">
      <input type="email" formControlName="Email" class="form-control"
        placeholder="example@example.com" ngModel>
    </div>
  </div>

  <div class="form-group row">
    <label for="password" class="col-sm-3 col-form-label">Password:</label>
    <div class="col-sm-3">
      <input type="password" formControlName="Password"  class="form-control"
        placeholder="*********" ngModel>
    </div>
  </div>

  <br>

  <p class="text-center">Already have an account? <span>
      <button (click)="goToSignIn();" class="unstyled-button">Sign In.</button>
    </span></p>

  <button type="submit" class="button button4">Sign Up</button>
</form>

注册.component.ts

export class RegisterComponent implements OnInit {

user: User;
registerForm: FormGroup;

constructor(private router: Router, private userService: RegisterService, private toastr: 
          ToastrService, private fb: FormBuilder) { }

ngOnInit() {
  this.registerForm = this.fb.group({
    FirstName: ['', Validators.required],
    LastName: ['', Validators.required],
    UserName: ['', Validators.required],
    Email: ['', Validators.required],
    Password: ['', Validators.required],
  })
}


onSubmit(){
  return this.userService.RegisterUser(this.registerForm.value).subscribe((data:any) => {
    this.toastr.success('User Registered Successfully');
  }, error => {
    this.toastr.error('Registeration Failed');
  })
}

}

最佳答案

这不是您遇到问题的 JavaScript 错误。这是来自 Entity Framework 的错误。

您的 DBContext 正在被两个线程同时使用。它可能被同一请求中的两个线程使用。

检查您的 DBContext 是否由于某种原因未声明为静态。

您需要分享您的 API 代码,以便我们调试您的问题。

关于javascript - 我无法使用 FormGroup Angular 6 发出发布请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641877/

相关文章:

javascript - jQuery live 无法按预期工作

html - CSS全屏背景-页面变化时跳转

html - 在 R 中进行网络抓取时如何修复 HTTP 403 错误?

javascript - 当我点击联系表单的发送按钮时,博客上没有任何反应

angular - 在 Angular2 中使用 viewContainerRef 获取子/子组件

javascript - 尝试禁用除了 JavaScript 中的一个输入控件之外的所有 input=text 字段

javascript - 我似乎无法引用 2 个模式来填充

javascript - 默认 $resource POST 数据

node.js - 如何在 Angular 中谨慎使用 localStorage

javascript - Jquery 中数据属性的替代方案