角度自定义验证 - 无法读取属性'查找'未定义的

时间:2017-10-27 05:15:23

标签: angular

代码正在产生以下错误,并且在页面加载后也会调用shouldBeUnique方法。在触摸该字段之前不应该调用它。

还不清楚为什么要返回4组对象而不是1。

full error message

控制台输出:

  

Angular正在开发模式下运行。致电enableProdMode()以启用生产模式。

     

(3)[{...},{...},{...}]

     

(3)[{...},{...},{...}]

     

(3)[{...},{...},{...}]

     

(3)[{...},{...},{...}]

     

错误类型错误:无法读取属性'找到'未定义的       在client-form.component.ts:52

ClientFormComponent上课:

export class ClientFormComponent {

client: any = {};
emails: any[];

form = new FormGroup({
  email: new FormControl('', 
  null,
  (control: AbstractControl) => this.shouldBeUnique(control)),

password: new FormControl('', Validators.required),
confirm: new FormControl('', Validators.required),
firstname: new FormControl('', Validators.required),
lastname: new FormControl('', Validators.required),
address: new FormControl('', Validators.required),    
phone: new FormControl('', Validators.required),
medical: new FormControl('', Validators.required)
});

constructor(public clientService: ClientService) { }


async shouldBeUnique(control: AbstractControl) : Promise<ValidationErrors | null> {

  this.clientService.getClientEmails().subscribe(email => {
  this.emails = email;
  console.log(this.emails);  
});

return new Promise((resolve, reject) => {                                                          

  if (this.emails.find(e => e.email == control.value))         
      resolve({ shouldBeUnique: true });
  else 
      resolve({ shouldBeUnique: false });                                
});

}

0 个答案:

没有答案