I'm trying to fetch data from Strapi using Axios for my Vite website, but I get an Error 404 message.
So far I figured out that my REACT_APP_API_URL is giving me undefined when I add "/server" at the end of the localhost address.
What could be the problem?
This is the error I'm getting from Axios:
code
:
"ERR_BAD_REQUEST"
config
:
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
message
:
"Request failed with status code 404"
This is what I get on the browser when I click the link in the .env file:
{"data":null,"error":{"status":404,"name":"NotFoundError","message":"Not Found","details":{}}}
In my .env file I have:
REACT_APP_API_URL = http://localhost:1337/server
This is the code I'm running:
import React, { useEffect, useState } from "react";
import "./Featured.scss";
import Card from "../Card/Card";
import axios from "axios";
useEffect(() => {
const fetchData = async () => {
try {
const data = await axios.get(process.env.REACT_APP_API_URL + "/products", {
headers: {
Authorization: "bearer " + process.env.REACT_APP_API_TOKEN,
},
});
console.log(data)
} catch (err) {
console.log(err);
}
};
fetchData();
}, [])