I have a class where with axios I make requests to the server. The idea is to call this class from my Home component and implement its methods , such as the getOperations method to obtain all the operations of a user. but by console I get this:
Here is the OperationsService class to make the request to the server.
import axios from "axios";
class OperationsService{
async getOperations() {
//return Promise.resolve(this.items);
try {
const data = await axios
.get("http://localhost:4000/api/operations/user/[email protected]")
.then((res) => {
return (res.data);
});
} catch (e) {
alert("Error al intentar obtener las operaciones");
}
}
}
export default OperationsService;
Here is the Home component where I call the OperationsService class .
import React from "react";
import axios from "axios";
import OperationsService from "./../../operationsService"
const Home = () => {
React.useEffect(() => {
getOperations();
}, []);
const getOperations = async () => {
let operationsService = new OperationsService();
const data = operationsService.getOperations();
console.log(data);
}
}
It can be the configuration
async/await
ofaxios
in the classOperationsService
.Assuming the API endpoint returns a key
data
, you can try the following:In the component
Home
:Hope this answer is helpful.