Home > Software engineering >  why cant I save data to database?
why cant I save data to database?

Time:12-16

Trying to insert form data by this function.

Here is my function:

saveTimeline() {
      let formData = new FormData();
      formData.append("title", this.title);
      formData.append("from", this.from);
      formData.append("to", this.to);
      formData.append("events[0].year", this.year);
      formData.append("events[0].image", this.softCopyUpload);
      formData.append("events[0].content", this.content);
      formData.append("events[0].audio", this.audioFile);
      console.log(formData);
      this.$axios
        .post(this.$API   "api/v1/admin/timelines/", formData, {
          headers: {
            Authorization: "Bearer "   this.token,
          },
        })
        .then((res) => {
          if (res) {
            console.log("success");
          }
        });
    },

Error: "Timeline title must not be set to empty"

Always getting this error. Why ?

Why append is not working ?

My JSON

{
    "title": "dvdvds",
    "from": 1990,
    "to": 2010,
    "events": [
        {
            "year": 1990,
            "image": "test.jpg",
            "content": "test content",
            "audio": "test.mp4"
        }
    ]
}

CodePudding user response:

I have no idea what kind of object "this" is, but are you sure that this.title has been set to something, and is not empty?

CodePudding user response:

Seems like you don't set the form data correctly. The docs suggest something like this for the browser:

const params = new URLSearchParams();
params.append('param1', 'value1');
params.append('param2', 'value2');
axios.post('/foo', params);
  • Related