Home > Enterprise >  Axios request doesn't return a promise / Uncaught TypeError: Cannot read properties of undefine
Axios request doesn't return a promise / Uncaught TypeError: Cannot read properties of undefine

Time:01-09

I have an api object that I create from axios with this line of code.

const api = axios.create({ baseURL: 'http://localhost:3006' });

Then I export this call with this line of code...

export const editPost = (id, post) => {
  api(`/posts/${id}`, { method: 'put', data: post });
};

However, in the file that I use the call, when I try to use a .then method on it, I receive an error saying

index.js:25 Uncaught TypeError: Cannot read properties of undefined (reading 'then')

Here is the use of the export

  const editThisPost = (authorName, category, blogText) => {
    editPost(id, { authorName, category, blogText }).then(res => {
      setPost(res);
      setEditing(false);
    });
  };

Doing some research that means that my export isn't returning a promise. Anyone know how to fix this?

CodePudding user response:

Your editPost method doesn't return a Promise to call .then on.

You should update it to return the promise:

export const editPost = (id, post) => {
    return api(`/posts/${id}`, { method: 'put', data: post });
};
  • Related