简体   繁体   English

用于检查可用性的 Angular 验证器

[英]Angular validator to check availability

I'm creating a Reactive Form in angular to register some devices.我正在以角度创建一个反应式表单来注册一些设备。 In this form I ask for a serial number, and I want to check if this serial number is already registered in my database.在这个表格中,我要求一个序列号,我想检查这个序列号是否已经在我的数据库中注册。

When I type in the input form it activates the function serialNumAvailability to display or not a message.当我在输入表单中输入时,它会激活函数 serialNumAvailability 以显示或不显示消息。 This function calls a service function called checkSerialNumAvailability which send a get request to my back-end.这个函数调用一个名为 checkSerialNumAvailability 的服务函数,它向我的后端发送一个 get 请求。 I made some console.log and used Postman to test the get request, and it seems to work fine on the back-end side.我做了一些console.log 并使用Postman 来测试get 请求,它在后端似乎工作正常。 But at the moment I type one character in the input field, I go back to my home page, and I can't understand why...但是此刻我在输入栏中输入一个字符,我回到我的主页,我不明白为什么......

Here is my HTML template verification:这是我的 HTML 模板验证:

<input formControlName="serialNum" type="text" class="form-control" placeholder="Ex: SL07101-BL" name="serialNum" required>
<div *ngIf="f.submitted " class="help-block mt-2 text-danger" i18n> Serial Number is required</div>
<div *ngIf="serialNum?.value.errors.serialNumAvailability" i18n> Serial Number already registered</div>

Then my component function serialNumAvailability :然后我的组件函数 serialNumAvailability :

serialNumAvailability(control: FormControl) {

    return Observable.timer(500).switchMap(() => {

      return this.portalService.checkAvailabilitySerialNum(control.value)
        .map(res => {
          if (res && res.length == 1 && res[0].serialNum) {
            console.log("ON RENVOIE TRUE");
            return { serialNumAvailability: true};
          }
          console.log("ON RENVOIE NULL");
          return null;
        });
    });}

My service function :我的服务功能:

checkAvailabilitySerialNum(term: string): Observable<Portal[]> {
    let portals: Portal[];

    if (!term.trim()) {// if search is empty
      return Observable.of([]);
    }

    return this.http.get<Portal[]>(this.serverURL + `portal/?serialNum=${term}`)
    .map(Portals => Portals.filter(portals => portals.serialNum === term)
    );
  }

And finally the Node side :最后是节点端:

router.get('/',
    function (req, res) {
        console.log("ON RENTRE BIEN DANS LA FONCTION : " + req.query.serialNum);
            return models.portals.findAll({
                attributes: ['serialNum'],
                where: {
                    serialNum: req.query.serialNum
                }
            }).then((serialNums) => {
                if (serialNums) {
                    serialNums = serialNums.map((serialNum) => {
                        serialNum = serialNum.toJSON();
                        console.log(serialNum);
                        return serialNum;
                    })
                }
                console.log("ON FAIT RES.SEND DE "+serialNums);
                res.send(serialNums);
            })
    })

I don't know why I'm redirected, if someone has encountered this problem, thanks for support :)我不知道为什么我被重定向,如果有人遇到这个问题,感谢支持:)

Finally the problem was solved.最后问题解决了。 The redirection was due to a token interceptor which handled my request.重定向是由于处理我的请求的令牌拦截器。 As it was not authorized to GET information, I simply added an exception to this interceptor for my URL.由于它未被授权获取信息,我只是为我的 URL 向这个拦截器添加了一个例外。

This looks like this :这看起来像这样:

if (req.headers.has(this.config.InterceptorSkipHeader)) {
      let headers = req.headers.delete(this.config.InterceptorSkipHeader);
      return next.handle(req.clone({ headers }));
    }
    if(this.authenticationService.isLoggedIn()){
      this.authenticationService.updateExpiration();
    }
    else if (this.router.routerState.snapshot.url != "/register-portal"){
      this.router.navigate(['/login'], { queryParams: { returnUrl: ''} });
    }

Thanks to those who answered :)感谢那些回答:)

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

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