1

I separated out an axios call into another file called ApiMethods.js.

Now, whenever I try to use the 'uploadSpecFile' function inside ApiMethods.js, I get this error:

Uncaught TypeError: Cannot read property 'then' of undefined

Here is that helper method:

export function uploadSpecFile(formData, engineId) {
    return
    axios({
        method: 'post',
        url: `/api/engineData/spec-files/${engineId}/files/upload`,
        data: formData,
        config: {
            headers: { 'Content-Type': 'multipart/form-data' }
        }
    })
}

And I use it here:

import {  uploadSpecFile } from '/ApiMethods';

// error occurs here  **
uploadSpecFile(bodyFormData, this.props.engineId).then(response => {

        this.setState({
            selectedFile: response.data,
            file: null
        });
    })
    .catch(response => {
    });

Is there something I'm doing wrong?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

1 Answers1

2

You are returning undefined, since axios is not on the same line as the return statement.

Just move the axios request to the same line and it will work as expected.

export function uploadSpecFile(formData, engineId) {
  return axios({
    method: "post",
    url: `/api/engineData/spec-files/${engineId}/files/upload`,
    data: formData,
    config: {
      headers: { "Content-Type": "multipart/form-data" }
    }
  });
}
Tholle
  • 108,070
  • 19
  • 198
  • 189