簡體   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