【问题标题】:TypeError: Cannot read property 'id' of undefined in dynamic parameters node jsTypeError:无法读取动态参数节点js中未定义的属性“id”
【发布时间】:2021-03-13 03:47:48
【问题描述】:

我正在尝试在节点 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)
});

【问题讨论】:

    标签: javascript node.js express server


    【解决方案1】:

    这里的错误是您的处理程序的参数顺序错误。按这个顺序应该是(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)
    });
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 这是公认的答案吗?它基本上是两小时前提交的答案的副本。
      【解决方案3】:

      我相信你交换了responserequest 参数。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-12
        • 2018-11-30
        • 2018-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-24
        相关资源
        最近更新 更多