1

I'm making a GET request with Axios in a React-Redux project, and I get the following error:

TypeError: "config.method.toLowerCase is not a function"

request     Axios.js:43 
wrap        bind.js:11 
apiCall     api.js:32 
apiCall     api.js:29 
...         ...

api.js is a file from my own project. bind.js and Axios.js is from the Axios library. This is my api function:

export function apiCall(method, path, data){
  let url = backendDomain + path
  let config = {
    method: [method],
    url: [url],
    data : [data],
    headers:{
      "Content-Type":"application/json",
      "Accept":"application/json"
    }
  }
   return new Promise((resolve, reject)=>{
     return axios(config).then(res=> {
      return resolve(res.data)
    }).catch(err => {
      return reject(err.response);
    })
  })

The function that makes use of apiCall() is this function:

export function authUser(url, userData, method){  
  return (dispatch) => {
    return new Promise((resolve, reject)=>{
      return apiCall(method, "/"+`${url}`, userData)
      .then((data) => {
        ...
        resolve();
      })
      .catch(err=>{
        ...
        reject();
      })
    })
  }
}

Do you think there's something wrong with my code or is there something wrong with the library? When I use authUser to dispatch my action (for Redux State), I double checked that "method" is a String, I console.logged typeof method in api.js, and it returned string.

Edit:

I tried calling toString() to the method parameter passed into apiCall(), but it didn't work:

  let reMethod = method.toString();
  const config = {
    method: [reMethod],
    url: [url],
    data : [data],
    headers:{
      "Content-Type":"application/json",
      "Accept":"application/json"
    }
  }
DP Park
  • 815
  • 1
  • 9
  • 19
  • Hi Bro! can you check this link might it will help you https://stackoverflow.com/a/39300840/7924858 – abhinavsinghvirsen Feb 22 '19 at 05:44
  • @abhinavxeon Hi, I tried changing the my apiCall() function. You can see how I made the changes in my post above (I edited it). It's still giving the same error. My Heroku App should also reflect the same changes. – DP Park Feb 22 '19 at 06:01
  • 2
    `[method]` and `[url]` at least should just be strings, not arrays. (`method` vs `[method]`) – lemieuxster Feb 22 '19 at 06:04
  • `.toLowerCase` is a string method, not an array method and in method, you're passing an array. – Daksh M. Feb 22 '19 at 06:10

1 Answers1

3

As mentioned in my comment, you're providing an array when axios expects a string:

  const config = {
    method: reMethod,
    url: url,
    data : data,
    headers:{
      "Content-Type":"application/json",
      "Accept":"application/json"
    }
  }
Daksh M.
  • 4,589
  • 4
  • 30
  • 46