Location: server/controllers/propertyController.js (Line 202)
Problem:
- The
getMyPropertiesendpoint was using.lean()which could cause issues with nested document serialization - Missing agent population, which could cause issues when frontend expects agent data
Fix Applied:
// BEFORE:
exports.getMyProperties = async (req, res, next) => {
try {
const properties = await Property.find({ agent: req.user.id })
.sort({ createdAt: -1 })
.lean();
res.status(200).json({ success: true, count: properties.length, properties });
}
};
// AFTER:
exports.getMyProperties = async (req, res, next) => {
try {
const properties = await Property.find({ agent: req.user.id })
.populate('agent', 'name email avatar phone bio role')
.sort({ createdAt: -1 });
res.status(200).json({
success: true,
count: properties.length,
properties: properties.map(p => p.toObject ? p.toObject() : p)
});
} catch (err) {
next(err);
}
};Impact: ✅ Properties now include complete agent information ✅ Better Mongoose document serialization ✅ Consistent JSON output format
Location: client/src/app/agent/dashboard/page.tsx
Problem:
- No error state management - users couldn't see API failures
- Empty state just showed "No properties added yet" without helpful context
- Loading state was too simple ("Loading...")
- Potential undefined field access (location.city, price without fallback)
Fixes Applied:
// Added to state:
const [error, setError] = useState<string | null>(null);
// Updated fetch:
setError(null); // Clear on new fetch
// ... in catch block:
setError("Failed to load properties. Please try again.");{error && (
<div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4 mb-4">
<p className="text-red-700 dark:text-red-400 text-sm">{error}</p>
</div>
)}<div className="flex justify-center items-center py-8">
<div className="w-8 h-8 border-4 border-primary/20 border-t-primary rounded-full animate-spin" />
</div><div className="text-center py-8">
<Building2 className="w-12 h-12 text-muted-foreground/30 mx-auto mb-3" />
<p className="text-muted-foreground">No properties added yet.</p>
<Link href="/agent/add-property" className="text-sm text-primary font-medium hover:underline mt-2 inline-block">
Add your first property
</Link>
</div>// Added safe field access with fallbacks:
<p className="text-sm text-muted-foreground">{prop.location?.city || 'N/A'} • PKR {(prop.price || 0).toLocaleString()}</p>
// Better responsive layout:
<div className="flex items-center gap-4 flex-1 min-w-0">
{/* Image */}
<div className="flex items-center gap-4 text-sm font-medium flex-shrink-0 ml-4">
{/* Stats */}
</div>
</div>Impact: ✅ Users can now see what went wrong if API fails ✅ Empty state is more helpful with CTA to add properties ✅ Better responsive design for property items ✅ No undefined field errors
File: server/routes/propertyRoutes.js
- ✅
/my-propertiesis defined BEFORE/:id - ✅ Express will match exact paths first (
/my-propertiesmatches as-is) - ✅ Only after that will parameterized routes (
/:id) be evaluated
Flow: Frontend → Axios Interceptor → Authorization Header → Backend Auth Middleware → Controller
Verified:
- ✅ Axios adds
Authorization: Bearer {token}header (lib/axios.ts) - ✅ Auth middleware validates token and user (middleware/auth.js)
- ✅ User role is checked with
authorize('agent', 'admin') - ✅ User.id is attached to req.user for property filtering
Flow: Dashboard → Link to /properties/{property._id} → Property Details Page
Verified:
- ✅ PropertyCard uses
property._idfor correct URL - ✅ Property type interface defines
_id: string - ✅ Property details page fetches using
params.id - ✅ MongoDB auto-generates
_idfor all documents
After deployment, verify these scenarios:
- Agent logs in and navigates to
/agent/dashboard - Recent Properties section loads and displays properties
- Click on a property → navigates to
/properties/{id} - Property details page loads correctly
- If no properties: "No properties added yet" message displays with CTA
- If API fails: Red error message appears
- Navigate to
/agent/properties - All agent's properties display in grid
- Each property card is clickable
- Approval status badge shows correct state
- View count displays properly
- Images load and gallery works
- Agent info displays correctly
- All fields (bedrooms, bathrooms, area) display
- Inquiry form works
-
Backend Changes:
- Populated agent data ensures frontend can access agent information if needed
- Removed lean() to avoid potential serialization issues with nested documents
- Added toObject() for consistent JSON serialization
-
Frontend Changes:
- Error state catches API failures and network issues
- Empty state provides helpful context instead of blank UI
- Safe field access prevents runtime errors from missing data
- Responsive layout works better on mobile devices
- Route ordering (already correct)
- Authentication middleware (working as intended)
- PropertyCard navigation (already using correct _id)
- Property model fields (all properly defined)
- Axios configuration (token already being sent)
Ensure these are set in your .env files:
Backend (.env):
PORT=5000
MONGODB_URI=mongodb://...
JWT_SECRET=your_secret
NODE_ENV=development
Frontend (.env.local):
NEXT_PUBLIC_API_URL=http://localhost:5000/api
- Run backend server:
cd server && npm run dev - Run frontend:
cd client && npm run dev - Test the complete flow from dashboard → properties → details
- Verify error handling by temporarily stopping backend
- Test with agent who has multiple properties and one with no properties
Last Updated: 2026-06-05 Status: ✅ COMPLETE - All issues identified and fixed