1

I am trying to make a dynamic parameters in node js where the link /eshop/edit/1111 will take you to edit a specific product but every time i try to add a product code after the /edit/ i get TypeError: Cannot read property 'id' of undefined

this is my server code

router.get('/edit/:id', async (res, req) =>
{
    let id = req.params.id;
    
    let data = {
        title: `Edit product ${code} | BuckStar`,
        pageTitle: `Edit product ${code}`,
        product: id
    };

    data.res = await results.editProduct(id, req.body.category, req.body.name, req.body.description, req.body.price, req.body.amount, req.body.amount, req.body.location);

    res.render("page/create-edit", data)
});
0

3 Answers 3

2

The error here is you have the arguments in the wrong order for your handler. It should be (req, res) => in that order.

Full code being:

router.get('/edit/:id', async (req, res) =>
{
    let id = req.params.id;
    
    let data = {
        title: `Edit product ${code} | BuckStar`,
        pageTitle: `Edit product ${code}`,
        product: id
    };

    data.res = await results.editProduct(id, req.body.category, req.body.name, req.body.description, req.body.price, req.body.amount, req.body.amount, req.body.location);

    res.render("page/create-edit", data)
});
Sign up to request clarification or add additional context in comments.

Comments

2

I think the correct code is this as the request should come before response

router.get('/edit/:id', async (req,res ) =>
{

});

1 Comment

How is this the accepted answer? It's basically a copy of an answer submitted two hours before it.
1

I believe you exchanged the response and request parameters.

See link below for Express documentation.

http://expressjs.com/en/guide/routing.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.