I have this submit function below to save an article.
My response code is 200 in browser but it triggers both then success branch and the catch error. I don’t understand why.
my article is saved correctly
from my browser log as you can see I get both 1 and 3 console.log() :
[Log] 1 (src_vue_component_role_common_article_add_vue-f9ecfd1edc9c94aaed1b.js, line 243)
[Log] 3 (src_vue_component_role_common_article_add_vue-f9ecfd1edc9c94aaed1b.js, line 259)
[Error] LOG – "There was an error: " – ""
Error (src_vue_component_role_common_article_articles-editor_vue-f9ecfd1edc9c94aaed1b.js:1560)
(anonymous function) (src_vue_component_role_common_article_add_vue-f9ecfd1edc9c94aaed1b.js:260)
submit() {
if (this.validate()) {
let formData = this.$refs["media-manager-" + this.uid]
? this.$refs["media-manager-" + this.uid].getFormData()
: new FormData();
formData.append("article", JSON.stringify(this.article));
this.loading = true;
let context = this;
this.progress = 0;
axios
.post(this.articlePath, formData, {
headers: { "Content-Type": "multipart/form-data" },
onUploadProgress: function (progressEvent) {
context.progress = Math.round(
(progressEvent.loaded * 100) / progressEvent.total
);
},
})
.then((response) => {
const data = response.data;
if (data.success) {
console.log(1);
toastr.success("article saved", null, {
positionClass: Storage.preferences.nPos,
});
const article = JSON.parse(data.article);
this.$emit("saved", article);
this.loading = false;
document.getElementById("contentPad").scrollIntoView();
this.progress = 0;
} else {
console.log(2);
Error(response);
this.loading = false;
this.progress = 0;
}
})
.catch((response) => {
console.log(3);
Error(response);
this.loading = false;
this.progress = 0;
});
}
},
>Solution :
You are checking if your Data.success is true. If it comes as undefined in your data, it is going to go into the else block and call Error.
Please check the data being sent