Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making a route to get details of all businesses #2

Open
mahendra785 opened this issue Aug 20, 2024 · 1 comment
Open

Making a route to get details of all businesses #2

mahendra785 opened this issue Aug 20, 2024 · 1 comment

Comments

@mahendra785
Copy link
Collaborator

Make a route which returns the details of all the businesses in the database, so that we can display it on the website.

@D-Vspec
Copy link
Collaborator

D-Vspec commented Aug 20, 2024

Implement Route to Get Details of All Businesses

Objective

Create a route that returns the details of all businesses from the database. This will allow you to display business information on the website.

Steps

1. Define the Route in the Backend

Express Route Handler Example:

const express = require('express');
const router = express.Router();
const Business = require('../models/businessModel');  // Assuming the schema is in `models/businessModel.js`

// GET route to fetch all businesses
router.get('/businesses', async (req, res) => {
    try {
        // 1. Fetch all businesses from the database
        const businesses = await Business.find();

        // 2. Return the businesses in the response
        res.status(200).json(businesses);
    } catch (error) {
        // 3. Handle any errors (e.g., database issues)
        res.status(500).json({ error: "Server error. Please try again later." });
    }
});

module.exports = router;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants