Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Axios put return undefined [React JS + Axios]

Hi all im doing an axios put to update my data. However im getting an undefined response. Thank you in advance

function that call the Axios:

export function exUpdateMovie(movie) {
 
  Axios.put(baseURL + "/api/update", {
    movieName: movie.movieName,
    movieReview: movie.movieReview,
    id: movie.id,
  })
    .then((response) => {
      // console.log(response);
      return response;
    })

    .catch((e) => {
      console.log(e);
      return e;
    });
}

function in app.js that calls the exUpdateMovie(movie) function:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const handleUpdateMovie = (movie) => {
    console.log("UpdateMovie Passed!");

    try {
      const res = exUpdateMovie(movie);   
      alert(res?.data);
    } catch (error) {
      console.log(error);
    }
  };

Output when I alert my response is:
undefined

SETTLE:

  • need to add async and await at the handleUpdateMovie
  • need to return the Axios by doing return Axio.put()

Cheers mate for helping me. Thanks alot

>Solution :

Because exUpdateMovie doesn’t return anything. Return the Promise:

export function exUpdateMovie(movie) {

  return Axios.put(/* all your Axios code */);

}

Then when consuming the result, treat it as a Promise:

exUpdateMovie(movie).then(res => {
  alert(res?.data);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading