Answer the question
In order to leave comments, you need to log in
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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question