类型错误:无法读取动态参数节点 js 中未定义的属性'id'



我正试图在节点js中创建一个动态参数,链接/eshop/edit/1111将带您编辑特定的产品,但每次我尝试在/edit/之后添加产品代码时,我都会得到TypeError: Cannot read property 'id' of undefined

这是我的服务器代码

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)
});

这里的错误是处理程序的参数顺序错误。按照这个顺序应该是(req, res) =>

完整代码为:

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)
});

我认为正确的代码是这样的,因为请求应该在响应之前出现

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

我相信您交换了responserequest参数。

有关Express文档,请参阅下面的链接。

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

最新更新