【问题标题】:Express get URL and delete certain object快速获取 URL 并删除某些对象
【发布时间】:2018-10-08 10:57:04
【问题描述】:

现在我正在使用 Express 和 LowDB 开发 CRUD(创建、读取、更新、删除)应用程序。

我成功开发了创建和读取但删除功能不起作用。

有这样的详情页

如您所见,有垃圾桶图标,实际上我的提交按钮也有一些问题,但这并不重要,所以我只制作提交按钮。

这是 pug 文件的一部分

aside#Delete.is-visible
  form(method="post" action='/delete_process')
   input(type='submit')
  a#DeleteButton(role='button')
    img(src='https://png.icons8.com/ios/35/000000/waste-filled.png' id="go_up")
    span.text
      | Delete
      br
      b this

详细页面有 URL(例如:DIR1WTqr2)和它的 DB id

和删除过程代码是这样的。

/* Delete Process */
router.post('/delete_process', function (req, res) {
    // GET Delete ID from URL
    let id=req.params.id;

    console.log(id);
    db.get('project').find({
      id: id
    }).remove().write();

});

我以为我可以获取 URL 并在 json 文件中找到带有 id 的数据。但它不起作用。当我试图控制 req.params.id 它说未定义

而且我认为与查看详细信息页面有冲突,因为详细信息页面代码是这样的。

/* GET Detail View Page */
router.get('/:id', (req, res, next) => {
  // GET URL params and put it into :id
  let id = req.params.id;
  // console.log(id);
  // GET data id to use Object
  let data = db.get('project').find({
    id: id
  }).value();
  // Get github URL
  let url = data.githuburl;
  // Use Request Module to parsing Web page
  request(url, function (error, response, html) {
    if (error) {
      throw error
    };
    // Parsing readme ID in github page
    var $ = cheerio.load(html);
    $('#readme').each(function () {
      // save to readme Variable
      let readme = $(this).html();
      // console.log(data);
      // console.log(id);
      res.render('detail', {
        dataarray: data,
        name: data.name,
        url: data.url,
        explanation: data.explanation,
        imgurl: data.imgurl,
        markdown: readme,
        startDate: data.pjdate1,
        endDate: data.pjdate2,
        githuburl: data.githuburl,
        sumlang: data.sumlang
      });
    });
  });
});

因为:id,当我单击 /delete_process 时,它会转到我认为的详细信息页面。

【问题讨论】:

    标签: javascript node.js express pug


    【解决方案1】:

    Pug 对您的缩进非常具体。您的按钮未包含在表单中 - 它是一个兄弟。当您希望一个元素包含在另一个元素中时,请确保始终有两个空格。

    将 pug 模板更改为此,按钮将正确提交:

    aside#Delete.is-visible
      form(method="post" action='/delete_process')
        input(type='submit')
          a#DeleteButton(role='button')
            img(src='https://png.icons8.com/ios/35/000000/waste-filled.png' id="go_up")
            span.text
              | Delete
              br
              b this
    

    但是等等还有更多!您没有将餐厅的 ID 与表单一起传回。

    我看不到您如何在哈巴狗中使用餐厅 ID,但您也需要以某种方式将其添加到表单中。假设它是 pug 模板中名为 restaurant.id 的变量。

    您的 node.js 路由处理程序正在尝试读取 req.params.id,这意味着它将查找 url 以获取餐厅的 id。您可以像这样将 id 添加到请求中:

    aside#Delete.is-visible
      form(method="post" action='/delete_process/' + restaurant.id)
        input(type='submit')
    

    然后,您还需要修改路由以包含 id 参数:

    router.post('/delete_process/:id', function (req, res) {
    

    另一种方法是添加一个隐藏的表单字段,该字段将 id 与表单一起发送(here's an article 解释了这个概念)。如果您这样做,您可以将表单更改为如下所示:

    aside#Delete.is-visible
      form(method="post" action='/delete_process/' + restaurant.id)
        input(type='hidden' name='id' value= restaurant.id)
        input(type='submit')
    

    那么你的删除处理路由可以像这样捡起来:

    router.post('/delete_process', function (req, res) {
        // GET Delete ID from form field
        let id= req.body.id;
    

    传递 id 的第一种方法(在 url 中)是 ajax 请求的标准,第二种方法(作为表单中的字段)是发布表单的标准。

    【讨论】:

      猜你喜欢
      • 2020-07-15
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多