0

in my Vuejs app, am trying to send an HTTP post to my server and I keep getting this error in the console,

TypeError: _api__WEBPACK_IMPORTED_MODULE_0__.default.post is not a function
    at Object.coursePix (services.js?87e7:5)
    at VueComponent._callee$ (testCourse.vue?8c89:98)
    at tryCatch (runtime.js?96cf:45)
    at Generator.invoke [as _invoke] (runtime.js?96cf:271)
    at Generator.prototype.(anonymous function) [as next] (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:97:21)
    at asyncGeneratorStep (asyncToGenerator.js?3b8d:5)
    at _next (asyncToGenerator.js?3b8d:27)
    at eval (asyncToGenerator.js?3b8d:34)
    at new Promise (<anonymous>)
    at new F (_export.js?63b6:36)

in the api.js which contains the server URL and the header which was set with axios interceptor

here is the api.js

import axios from 'axios'

export default () => {
    const ajax = axios.create({ 
        baseURL: `http://localhost:8081/`
    })

    ajax.CancelToken = axios.CancelToken
    ajax.isCancel = axios.isCancel

    ajax.interceptors.request.use(
        (config) => {
            let token = localStorage.getItem(AUTH_TOKEN)
            if (token) {
                config.headers['Authorization'] = `Bearer ${ token }`
            }
            return config
        },
        (error) => {
            return Promise.reject(error)
        }
    )
    return ajax
}

here is the services.js

import api from '@/api'

export default {
    coursePix (credentials) {
        return api.post('coursePix', credentials)
    }
}

and the upload.vue

async AddCourse(){
    const formData = new FormData()
    formData.append('file', formData)
    var obj = {
        send: 'send'
    }
    try{                
        await services.coursePix(obj)
    }
    catch(err){
       console.log(err)
   }
}

I tried everything I could but it's not working, how can I solve this issue or what am I doing wrong

1 Answer 1

1

Seems like you didnt retrieve the ajax instance:

return api.post('coursePix', credentials)

instead of:

return api().post('coursePix', credentials)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.