Search code examples
node.jsexpressvue.jssuperagent

Vue-Superagent.post is not a function


I'm using vue-superagent to upload a file. I have this code that was working before however now I get the error:

vue_superagent__WEBPACK_IMPORTED_MODULE_5___default.a.post is not a function

//@ts-ignore
import VueSuperagent from "vue-superagent";

onUpload(): void {
    if(this.fileChosen){
      const fd = new FormData();
      fd.append("file", this.selectedFile, this.selectedFile.name);
      VueSuperagent.post("http://localhost:8080/routes").attach("file", fd);
    } else {
      this.fileMsg = "You haven't chosen a file";
    }
  }

I can't figure out what I changed in my code to get this error. It felt like it was working one day and then the day after it gave me this error. That's why I'm kind of lost in how to fix this.

I've tried reinstalling the package but it keeps giving me the error.


Solution

  • As it's shown in the documentation, it's expected to be used as:

    import Vue from 'vue'
    import VueSuperagent from 'vue-superagent'
    
    Vue.use(VueSuperagent)
    
    ...
    
    Vue.superagent.post(...)
    

    Otherwise it doesn't make sense to use vue-superagent, if the intention is to use superagent apart from Vue then superagent should be used directly.