【问题标题】:send variable from pug to server将变量从 pug 发送到服务器
【发布时间】:2022-01-23 23:36:41
【问题描述】:

此代码显示数据库中的医生列表,在用户选择医生后,我将他重定向到“/user/new_diagnosis/doctor/”页面,我想要发送用户选择的医生 ID服务器,所以我可以在“/user/new_diagnosis/doctor/”页面中使用它,请知道我该怎么做

server.js

app.get("/user/new_diagnosis",checkNotAuthenticated,(req, res) => {
    db.query(
      `SELECT * FROM doctor`,
        (err, results) => {
          if (err) {
            console.log(err);
          }
           res.render("user-diagnosis",{doctors: results, name: req.user.userName ,surname: req.user.userSurname });
        }
      );

  });
  
  app.get("/user/new_diagnosis/doctor",checkNotAuthenticated,(req, res) => {
    res.render("user-diagnosis-doctor");
  

  });

.pug

extends user-layout 

block content 
    div.container-fluid
        div(class=["row", "pt-4"])
            div(class=["col-md-3", "col-sm-5", "d-md-flex", "flex-column", "border-end"])
                h3(class=["text-black", "py-4", "text-center"]) Select A Doctor
                form.d-flex.mb-3(role="form", action="/", method="get")
                    input.form-control.me-2(id="doctor-search", name="doctor-search", type='search' placeholder='Search' aria-label='Search')
                    button.btn.btn-outline-success(type='submit') Search 

                each doctor  in doctors
                    .list-group
                        a.list-group-item.list-group-item-action.d-flex.gap-3.py-3(href= `/user/new_diagnosis/doctor/`, aria-current='true')
                            svg(xmlns="http://www.w3.org/2000/svg", width="50", height="50", fill="currentColor", class=["bi", "bi-person-circle", "pe-1"], viewBox="0 0 16 16")
                                path(d="M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z")
                                path(fill-rule="evenodd", d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z")
                            .d-flex.gap-2.w-100.justify-content-between
                                div
                                    h6.mb-0= "Doctor "+ doctor.doctorName +" "+ doctor.doctorSurname
                                    p.mb-0.opacity-75= doctor.hospitalName
                                small.opacity-50.text-nowrap now
            div(class=["col-md-9", "col-sm-7", "px-4"])
                //-form(role="form", action="examination_card", method="post")

【问题讨论】:

  • 如果这样做,经典的方法是 GET /doctors/:doctorID。你可以在 Node/Express 中拥有一个类似 app.get("/doctors/:doctorID", (req, res) => ... 的路由,然后你在 req.params.doctorID 中获得 ID。然后,您可以在数据库中查找文档,获取完整的 Doctor 对象,使用它来呈现 Pug 模板,最后将呈现的模板(即 HTML)作为响应发送到浏览器。

标签: javascript node.js express frontend pug


【解决方案1】:

由于您没有将doctor.id 传递给前端,因此无法访问它。

要解决此问题,您可以将医生的 ID 添加到 .pug 中的锚链接中

href=`/user/new_diagnosis/doctor/`+doctor.id

然后在 server.js 中,更新您的端点以获取 id

app.get("/user/new_diagnosis/doctor/:doctorId", ...

最后,访问变量

req.params.doctorId

免责声明:此解决方案未经测试,但应该会引导您走上正确的道路。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多