Troubleshooting
Relevant source files
The following files were used as context for generating this wiki page:This document provides guidance for diagnosing and resolving common issues within the LostData application. It covers problems related to server startup, database connectivity, API errors, and frontend behavior. The application consists of a Node.js/Express backend, a MongoDB database, and a vanilla JavaScript frontend.
Troubleshooting generally involves checking server console logs for startup messages and runtime errors, inspecting browser developer tools for network request failures and client-side errors, and verifying database state and configuration.
Server Startup and Connectivity
Issues in this area typically prevent the application from running or connecting to its database.
Server Won't Start
If the Node.js server fails to start, check the following:
- Dependencies: Ensure all required npm packages are installed by running
npm install. The necessary dependencies are listed inpackage.json.
Sources: package.json - Environment Variables: The server requires a
MONGODB_URIenvironment variable to connect to the database. If this is not set, it falls back to a default local URI. The server port can be set via thePORTenvironment variable, defaulting to3000.
Sources: server.js:2, server.js:143, APP/config/db.config.js:2 - Console Logs: Check the console for error messages. A successful startup will log "DB connected" followed by "Server on [PORT]".
Sources: server.js:26, server.js:144
The diagram below illustrates the server startup sequence.
Sources: server.js:2-144
Database Connection Failure
A failure to connect to MongoDB is a common startup issue.
- Connection String: The connection URI is defined by
process.env.MONGODB_URIor defaults tomongodb://127.0.0.1:27017/model_db. Verify that the MongoDB instance is running and accessible at this address.
Sources: APP/config/db.config.js:2, server.js:25 - Error Logging: Any connection error is caught and logged to the console. This log will contain details about the connection failure.
Sources: server.js:25
API Endpoint Errors
The backend exposes a REST API for managing models. Errors can occur due to invalid requests, lack of permissions, or server-side problems. All API routes are defined in server.js.
Authentication and Authorization
Most write operations (create, update, delete) and data management features (import/export) are protected and require admin privileges.
- Admin Role: A user is considered an 'admin' if their login email ends with
@admin.com. Otherwise, they are a 'chatter'.
Sources: server.js:30 - Admin Middleware: The
isAdminmiddleware checks ifreq.session.user.roleis 'admin'. If not, it returns a403 Forbiddenstatus with the message 'Admin required'.
Sources: server.js:40-43
The following sequence diagram shows a failed request to an admin-only endpoint by a non-admin user.
Sources: server.js:40-43, server.js:77, public/index.html:141-151
API Endpoint Error Summary
All API endpoints include try...catch blocks that respond with a 500 Internal Server Error if an unhandled exception occurs on the server.
| Endpoint | Method | Admin | Description | Common Errors |
|---|---|---|---|---|
/api/login | POST | No | Logs a user in and sets their role in the session. | N/A (creates session) |
/api/logout | POST | No | Destroys the user session. | N/A (destroys session) |
/api/models | GET | No | Fetches all models. | 500 on database query failure. |
/api/models/:id | GET | No | Fetches a single model by ID. | 500 on database query failure. |
/api/models | POST | Yes | Creates a new model. | 403 if not admin. 500 on database save error (e.g., validation). |
/api/models/:id | PUT | Yes | Updates an existing model. | 403 if not admin. 500 on database save error. |
/api/models/:id | DELETE | Yes | Deletes a model. | 403 if not admin. 500 if model not found or DB error. |
/api/export/json | GET | Yes | Exports all models as JSON. | 403 if not admin. 500 on database query failure. |
/api/export/csv | GET | Yes | Exports all models as CSV. | 403 if not admin. 500 on database query failure. |
/api/import/csv | POST | Yes | Imports models from a CSV file. | 403 if not admin. 500 if file parsing or insertMany fails. |
Sources: server.js:28-132
Database and Data Integrity
Issues can arise from incorrect data schemas, failed data seeding, or validation errors during create/update operations.
Model Schema
The Model schema is defined using Mongoose. All data operations must conform to this schema.
This ER diagram shows the structure of the Model collection.
Sources: APP/models/model.model.js:2-15
Key fields and constraints are listed below:
| Field | Type | Constraints | Description |
|---|---|---|---|
id | String | required, unique | A unique identifier for the model. |
name | String | required | The name of the model. |
profile | String | required | The model's profile description. |
pricing | String | required | Pricing information. |
dosAndDonts | String | required | List of allowed and disallowed activities. |
history | Array | default: [] | An array tracking changes to the record. |
Sources: APP/models/model.model.js:2-15
A common issue is a 500 error on POST /api/models if required fields are missing from the request body or if the id is not unique.
Data Seeding
The project includes a seed.js script to populate the MongoDB database with initial data. If the application shows no models, it may be because the database is empty.
- Running the Seeder: Execute
node seed.jsto populate the database. - Seeder Logic: The script connects to the MongoDB instance defined in
db.config.js, then usesModel.insertMany()to add a predefined list of models. - Error Handling: Any errors during connection or insertion are logged to the console.
Sources: seed.js
Note: The repository also contains a Python script, model_database.py, which manages a local models.json file. This is a separate utility and does not interact with the MongoDB database used by the main Node.js application.
Sources: model_database.py
Frontend Issues
Client-side problems usually manifest as missing data, non-functional buttons, or incorrect UI states. Use the browser's developer tools (Console and Network tabs) for diagnosis.
Login and UI State
The user's role (admin or chatter) is stored in localStorage after a successful login. This role determines the visibility of admin-only UI elements like the "Add Model", "Edit", and "Delete" buttons.
- Symptom: Admin buttons are not visible after logging in with an admin email.
- Cause: The
localStorage.setItem('role', data.role)call may have failed, or the role was not correctly determined by the backend. - Troubleshooting:
- Check
localStoragein the browser's developer tools to see if therolekey is set toadmin. - Inspect the response from
POST /api/loginin the Network tab to ensure it returns{ "role": "admin" }.
- Check
The following diagram shows the client-side login and data fetching flow.
Data Display and Search
- Symptom: The model grid shows "Loading..." indefinitely or is empty.
- Cause: The
fetch('/api/models')call might be failing, or it's returning an empty array. - Troubleshooting:
- Check the browser's Network tab for the
GET /api/modelsrequest. Verify its status code (should be200 OK) and response payload. - Check the browser's Console for any JavaScript errors occurring within the
fetchModelsorrenderModelsfunctions.
- Check the browser's Network tab for the
- Symptom: Search functionality is not working correctly.
- Cause: The search filter logic is case-sensitive or the search term doesn't match the
nameorprofilefields. The filtering is performed client-side on the already-fetched data.
Sources: public/index.html:128-135