im stuck i built an api with express and im trying to retrieve a games array, i can see it on postman everything is working fine and also in my mongodb the array is not empty but when i try to retrieve it in my front end i have an empty array . Do you have an idea on how to solve this ?
Here’s my backend api call and how i render it on the front end:
BACKEND :
app.get("/getAllGames", authenticateToken, async (req, res) => {
const { user } = req.user;
try {
// Récupère toutes les propositions de jeu pour l'utilisateur connecté, triées par statut "épingle"
const games = await Game.find({ userId: user._id }).sort({ isPinned: -1 });
console.log(games);
// Envoie une réponse avec toutes les propositions de jeu
return res.json({
error: false,
games,
message: "All games proposal retrieved successfully",
});
} catch (error) {
return res.status(500).json({
error: true,
message: "Internal Server Error",
});
}
});
FRONTEND :
// get All Games proposal
const getAllGames = async () => {
try {
const response = await axiosInstance.get("getAllGames");
console.log("Response Data:", response.data); // Log the entire response data
if (response.data && response.data.games) {
console.log("Games Array:", response.data.games); // Log the games array specifically
setAllGames(response.data.games);
} else {
console.error("No games found in the response");
}
} catch (error) {
console.error("An unexpected error occurred, Please try again.", error);
}
};
New contributor
Haydeer_ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.