Issue with Fetching Files in React – Getting 'Failed to Fetch' Error #1751
-
Help with Fetching Files in ReactHi everyone, I'm working on a React app where I'm trying to fetch files from a backend server. However, I'm encountering an issue where the fetch call fails, and I receive the following error message in the console: Here's the relevant code:import { useState, useEffect } from "react"
import { Table, TableHeader, TableBody, TableHead, TableRow, TableCell } from "@/components/ui/table"
interface File {
name: string
path: string
}
export default function Home() {
const [files, setFiles] = useState<File[]>([])
const [error, setError] = useState<string | null>(null)
const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
const fetchFiles = async () => {
setIsLoading(true)
try {
console.log("Fetching files...")
const response = await fetch("http://localhost:5000/api/files")
console.log("Response received:", response)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
console.log("Data received:", data)
setFiles(data)
} catch (e) {
console.error("Error fetching files:", e)
setError(`Failed to fetch files: ${e.message}. Please check if the backend server is running.`)
} finally {
setIsLoading(false)
}
}
fetchFiles()
}, [])
if (isLoading) {
return <div className="container mx-auto p-4">Cargando...</div>
}
if (error) {
return <div className="container mx-auto p-4 text-red-500">{error}</div>
}
return (
<div className="container mx-auto p-4">
<h1 className="text-2xl font-bold mb-4">Archivos en el Escritorio</h1>
{files.length > 0 ? (
<Table>
<TableHeader>
<TableRow>
<TableHead>Nombre</TableHead>
<TableHead>Ruta</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{files.map((file, index) => (
<TableRow key={index}>
<TableCell>{file.name}</TableCell>
<TableCell>{file.path}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
) : (
<p>No se encontraron archivos.</p>
)}
</div>
)
} What I've tried: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Response to "Issue with Fetching Files in React – Getting 'Failed to Fetch' Error"Hi! The "Failed to fetch" error you are encountering could be caused by several factors. Here are some steps and checks to help you debug the issue: 1. Check the Backend Server
2. CORS Issues
3. Network Errors
4. Error Handling
5. Server Logs
6. Use
|
Beta Was this translation helpful? Give feedback.
Response to "Issue with Fetching Files in React – Getting 'Failed to Fetch' Error"
Hi!
The "Failed to fetch" error you are encountering could be caused by several factors. Here are some steps and checks to help you debug the issue:
1. Check the Backend Server
Ensure that your backend server running at
http://localhost:5000
is up and running. You can check this by accessinghttp://localhost:5000/api/files
directly in your browser or using a tool like Postman to see if the API is responding correctly.If the server isn't running, make sure to start it before making requests from the frontend.
2. CORS Issues
If your backend is running on a different port (like
5000
) while your React …