# Troubleshooting Guide

## "Failed to fetch" Error

If you see "Failed to fetch" when trying to sign up or log in, follow these steps:

### 1. Check Backend Server is Running

The backend server must be running on port 3001. To start it:

```bash
cd backend
npm run dev
```

You should see:
```
🚜 Agriculture Robot Control Server running on port 3001
📡 WebSocket server ready for connections
🤖 Robot simulator initialized
🔌 Hardware connection service ready
```

### 2. Check Frontend Server is Running

The frontend should be running on port 3000:

```bash
cd frontend
npm run dev
```

### 3. Verify Ports

- Backend: `http://localhost:3001`
- Frontend: `http://localhost:3000`

Make sure no other applications are using these ports.

### 4. Check Browser Console

Open browser developer tools (F12) and check the Console tab for detailed error messages.

### 5. Common Issues

#### Backend Not Running
**Error**: "Failed to fetch" or "Cannot connect to server"
**Solution**: Start the backend server with `npm run dev` in the backend directory

#### CORS Error
**Error**: "CORS policy" in console
**Solution**: The CORS is configured, but if you see this error, make sure:
- Backend is running on port 3001
- Frontend is running on port 3000
- You're accessing via `localhost` not `127.0.0.1` (or update CORS config)

#### Port Already in Use
**Error**: "Port 3001 is already in use"
**Solution**: 
- Find and stop the process using the port
- Or change the port in `backend/.env` file

#### Missing Dependencies
**Error**: Module not found errors
**Solution**: 
```bash
cd backend
npm install

cd ../frontend
npm install
```

### 6. Test Backend Connection

Test if the backend is accessible:

Open in browser: `http://localhost:3001/health`

You should see:
```json
{
  "status": "healthy",
  "timestamp": "...",
  "service": "agri-robot-backend"
}
```

### 7. Test Auth Endpoint

Test the signup endpoint directly:

```bash
curl -X POST http://localhost:3001/api/auth/signup \
  -H "Content-Type: application/json" \
  -d '{
    "email": "test@example.com",
    "password": "test123",
    "firstName": "Test",
    "lastName": "User",
    "farmName": "Test Farm"
  }'
```

### 8. Clear Browser Cache

Sometimes cached data can cause issues:
- Clear browser cache
- Try incognito/private mode
- Clear localStorage: `localStorage.clear()` in browser console

### 9. Check Network Tab

In browser developer tools:
1. Go to Network tab
2. Try to sign up
3. Check if the request to `http://localhost:3001/api/auth/signup` appears
4. Check the status code and response

### 10. Firewall/Antivirus

Sometimes firewall or antivirus software blocks local connections. Temporarily disable to test.

## Still Having Issues?

1. Check backend terminal for error messages
2. Check frontend terminal for error messages
3. Check browser console for detailed errors
4. Verify all dependencies are installed
5. Make sure you're using the correct ports

## Getting Better Error Messages

The updated error handling will now show:
- "Cannot connect to server. Please make sure the backend server is running on port 3001." - if backend is not running
- Specific server errors with status codes
- Network error details

