【发布时间】: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