繁体   English   中英

如何从数据库中的标签“ a”中插入值以属性href

[英]How to insert value from the database in tag “a” to attribute href

如何将ID传递给href? 我通过req.params返回它,它返回未定义。 我需要从此URL获取ID,以使用findOne方法访问数据库中的数据。

我试过在哈巴狗文档中找到它,但是什么也没有。 我知道,mb这个问题很愚蠢,但是idk怎么做。 请帮忙。 码:

我的哈巴狗代码:

  div(class = "container")
    for item in items
      div
        a(href='/routes/#{item._id}')= item.InputItemName //this is one of the many variations of my attempts

controller.js

const {ObjectID} = require('mongodb'); //get objectid method
router.get('/:itemId', async (req, res) =>{
    const client = req.app.client;
    const db = client.db('MyDB'); //cnn to db
    const collection = db.collection('items'); //cnn to collection items
    var req_id= req.params.itemId; 
    console.log(req_id); //undefined
    const item = await collection.findOne({ _id: ObjectID(req_id)}); //not working, coz req_id = undefined
    console.log(item);
    if(!item) return res.redirect('/');
    res.render('routes/views/item', {item}); 
});

我解决了这个问题。 如果有人遇到这种情况:U需要在循环中初始化var,如下所示:

  div(class = "container")
    for item in items
      div
        img(src='firs.png', alt='image')
        - var url = item._id;
        a(href='/routes/' + url)=  item.InputItemName

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM