D
D
Dmitry Baskakov2020-01-17 09:11:44
Regular Expressions
Dmitry Baskakov, 2020-01-17 09:11:44

Why do Vue body variables remain empty after a fetch request?

After a request to the API, there is a response with json data, containing at least an array with the status of the request and a note. But after request data is not saved to variable from Vue body

Here is the code

new Vue({
        el: '#app',
        data: function () {
            return {
                firstChildSetting: {},
                secondChildSetting: {},
                firstChild: 'none',
                secondChild: 'none',
                currentSetting: {},
                client: 'none',
                formType: 'none',
                errorMessage: '',
                lastRequestResult: null,
            }
        },
        methods: {
            firstChildOnChange: function () {
                this.formSettingRequest(this.firstChild, 1);
            },
            secondChildOnChange: function () {
                this.formSettingRequest(this.secondChild, 2);

            },
            formTypeOnChange: function () {
                this.formSettingRequest(this.formType);
            },
            formSettingRequest: function (formType, childrenCount = 0) {
                fetch('http://site.name/api/v1/getFormConfig?type=' + encodeURIComponent(formType))
                    .then((response) => {
                        if (response.ok) {
                            return response.json();
                        } else {
                            throw new Error('Network response was not ok');
                        }
                    })
                    .then((response) => {
                        if (childrenCount === 2) {
                            this.firstChildSetting = response.data;
                        } else if (childrenCount === 1) {
                            this.secondChildSetting = response.data;
                        } else {
                            this.currentSetting = response.data;
                        }
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            clientOnChange: function () {

            }
        }
    });

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
DevMan, 2019-04-05
@Hlay

https://regex101.com/r/E6p09d/2

V
vreitech, 2019-04-05
@fzfx

well, for example:
https://regex101.com/r/OLL2U7/1

V
Valentin, 2019-04-05
@romanko_vn

[^\d\.]+

A
Aetae, 2020-01-18
@Aetae

Everything works: https://codesandbox.io/s/condescending-snow-feqp6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question