Calling Controller method and get the function print

Asked

Viewed 54 times

1

When I call the Controller Method to list users, instead of returning me the array it returns me the print of the function

Controller code:

export default userController = {
    Listar: () => {
      axios.get('http://localhost:8000/user').then(function(resposta){
        return resposta.data;
      })
    }
}

Giving the console.log to see the return:

    componentDidMount(){
      console.log(userController.Listar());
    }

Will someone please tell me why?

1 answer

1


First check if your controller is imported in this document for example:

import userController from "./userController";

Then put the console log inside the function to see if it returns.

    componentDidMount() {
        getUserData();
    }

    export function* getUserData() {
      try {
         LoadingActions.uiStartLoading();

        //chamada a API para capturar o user
        const data = yield call(api.get, "/api/logged_user");

        console.log("resposta", data); <---- SEU CONSOLE LOG

        yield UsersActions.getUserRequest(data);

        //Disparando a action de loading
        LoadingActions.uiStopLoading();
      } catch (error) {
         console.log("TCL: getUserData -> error", error);

         yield put(
         UsersActions.getUserFailure("Não foi possível buscar dados do usuário!")
    );
  }

Hugs

  • 1

    I was able to solve the problem by giving a Return before Axios.get return axios.get('http://localhost:8000/user').then(function(resposta){&#xA; return resposta.data;&#xA; })

  • Ahh yes make sense, I sent this example which is like an async/await but a Generator function that also makes a return. I advise to make a Try/catch in case this request fails. Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.