I'm trying to fetch data on my client-side from the server-side which is connected to MongoDB.
I'm using React on the front end and Axios for the HTTP requests.
I have 2 files, one for the API and one is the index.jsx of the app.
I successfully fetched the data from the DB but the result I get on the index.jsx is always undefined.
The API FILE:
export async function getNotesFromDB(googleId) {
let answer;
await axios
.get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
.then((notesDB) => {
answer = notesDB;
})
.catch((error) => {
//Indicates the client of an error getting the notes from
console.log(error);
answer= null;
})
.finally( () => {
return answer;
});
}
The index.jsx file :
import { getNotesFromDB as getNotesFromAPI } from "../API/Notes.jsx";
async function getNotesFromDB() {
if (userInfo) {
let googleId = userInfo.googleId;
const result = await getNotesFromAPI(googleId);
console.log(result);
} else {
history.push("/");
}
};