异步并等待不工作

时间:2019-08-05 15:39:12

标签: javascript vue.js async-await

它不等待验证,仅运行else部分:|我的错误在哪里?

  async validateBeforeSubmit(event) {
        await  this.$validator.validateAll().then(  result => {
            if (result) {
                 console.log(result); // just log the -> true
                // go submit
            }else{
                console.log(result);  // just log the -> false
                event.preventDefault();
                var elmnt = document.getElementById("drop_zone");
                elmnt.scrollIntoView();
            }
        })
        .catch(error=>console.log(error));
    },

我正在使用veevalidator,并且我定义了一些自定义规则,需要几秒钟才能解决:


 created() {
        this.$validator.extend('unique', {
            //   getMessage: field => 'At least one ' + field + ' needs to be checked.',
            async validate(value, arg) {
                arg = arg[0];
                let sw = false;
                if (arg == 'n_code') {
                    let data = {
                        'n_code': value
                    }
                    await Axios.post(duplicate_ncode, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                if (arg == 'email') {
                    let data = {
                        'email': value
                    }
                    await Axios.post(duplicate_email, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                if (arg == 'mobile') {
                    let data = {
                        'mobile': value
                    }
                    await Axios.post(duplicate_mobile, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                // console.log('questions', value, testProp, options.some((option) => option[testProp]));
                // return true;
            }
        });
    }

当用户填写所有字段时,它将检查3个api,并且需要momnets进行检查。 而且我需要等待得到答案,但是出现了一些错误,这是行不通的。

请帮助

1 个答案:

答案 0 :(得分:0)

我相信您想要做的是:

async validateBeforeSubmit(event) {
  try {
    const result = await this.$validator.validateAll();
    if (result) {
      // go submit
    } else {
      event.preventDefault();
      var elmnt = document.getElementById('drop_zone');
      elmnt.scrollIntoView();
    }
  }
  catch (e) {
    console.log(e);
  }
},