A
A
Apostol632020-06-11 13:27:06
Vue.js
Apostol63, 2020-06-11 13:27:06

How to continue executing code only after axios?

Good day everyone.
I hastily wrote this simple validator:

validateNewUserInfo() {
          this.errors = [];
            if(!this.newUserData.name) {
                this.errors.push('Укажите имя');
            }
            if(!this.newUserData.lastName) {
                this.errors.push('Укажите фамилию');
            }
            if(!this.newUserData.secondName) {
                this.errors.push('Укажите отчество');
            }
            if(!this.newUserData.phone) {
                this.errors.push('Укажите телефон');
            }
            if(!this.newUserData.password) {
                this.errors.push('Укажите пароль');
            }
            if(!this.newUserData.login) {
                this.errors.push('Укажите логин');
            }
            if(!this.newUserData.email) {
                this.errors.push('Укажите почту');
            } else if(!this.validMail(this.newUserData.email)) {
                this.errors.push('Укажите корректный адрес электронной почты');
            }
            if(!this.newUserData.role) {
                this.errors.push('Выберите роль');
            }
            var data = {};
            data = {'login': this.newUserData.login};
            axios.post('checkfield', {params: data}).then(response => {
                if(response.data !== 1) {;
                    this.errors.push(response.data);
                }
            });
            if(!this.errors.length) {
                return true;
            }

        },


At the end, the number of elements in the errors array is checked, and if they are not there, then true is returned. However, if an element gets into this array through axios (means that such a login exists), then true will already be returned. How can I deal with this problem?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-06-11
@Fragster

axios.post returns a promise, https://learn.javascript.ru/promise
after it becomes clear what it is, you can switch to async https://learn.javascript.ru/async-await

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question