Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

POST request triggers both THEN and CATCH

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading