bleumarine22
bleumarine22

Reputation: 1

Node, Express, 404 error / cannot get route

I am building an Express app and having some issues with routing. All of my routes are working perfectly except for one.

I have a route/admin/products that works:

const router = express.Router();

router.get("/admin/products", (req, res) => {
    res.send(productsIndexTemplate({ products }));
});

Another route/admin/products/new that also works:

router.get("/admin/products/new", requireAuth, (req, res) => {
    res.send(productsNewTemplate({}))
});

but this route doesn't work:

router.get('/admin/products/:id/edit'), async (req, res) => { 
    const product = await productsRepo.getOne(req.params.id);
    res.send(productsEditTemplate( {product} ));
};

I get the proper URL (meaning that I get a proper ID in the url so the req.params.id is not undefined or null but I have this message:

Cannot GET /admin/products/08a4dc4f/edit

I don't think the problem is with my code as the other routes are working except the last one.

Upvotes: 0

Views: 661

Answers (1)

Nikhil G
Nikhil G

Reputation: 2466

You added a parenthesis(closing bracket) in route.

router.get('/admin/products/:id/edit'), async (req, res) => { //<---- Your error is in this line. After edit you added closing bracket.
    const product = await productsRepo.getOne(req.params.id);
    res.send(productsEditTemplate( {product} ));
};

Replace with below code.

router.get('/admin/products/:id/edit', async (req, res) => { 
    const product = await productsRepo.getOne(req.params.id);
    res.send(productsEditTemplate( {product} ));
});

Upvotes: 1

Related Questions