Search code examples
vue.jsnullvuexmultipartform-datadataform

Why DataForm() in vue js null?


I have an event code I run it using console.log() and found the value of the console.log I run is [] why, i need help from master code vue, please help me

uploadFile() {
  let file = this.file;
  const filedata = new FormData();
  filedata.append("name", "my-file");
  filedata.append("file", file);

  console.log(filedata);

  this.$store.dispatch("postDataUpload", { filedata }).then(
    (response) => {
      // this.data = response.data
    },
    (error) => {}
  );
},

Screenshot


Solution

  • Your FormData object actually isn't empty, it's just that the data it contains isn't shown in the console like it would were it a plain object. You have to extract the entries as an array to see the data it contains.

    const d = new FormData()
    d.append('foo', 'bar')
    
    // Logging the object directly isn't very helpful
    // (data isn't shown in console)
    console.log(d)
    
    // Log the [key, value] entries as an array
    console.log([...d.entries()])

    Refer to the documentation for methods to access the data it contains. It's a map-like object, so expect similar methods.