使用 Express、Mongodb 和 EJ 显示下拉动态数据
Posted
技术标签:
【中文标题】使用 Express、Mongodb 和 EJ 显示下拉动态数据【英文标题】:Displaying dropdown dynamic data with Express, Mongodb and EJs 【发布时间】:2022-01-09 18:47:08 【问题描述】:我正在尝试使用下拉输入元素将我的猫鼬模型中的性别数据解析为 Ejs,但我不知道该做什么了 请问如何使用下拉菜单将数据从猫鼬模型解析到 Ejs 模板? 这是我的猫鼬模型
const express = require("express");
const mongoose = require("mongoose");
const Schema = mongoose;
const doctorSchema = new Schema(
fullname:
type: String,
required: true,
,
email:
type: String,
unique: true,
required: true,
,
birth:
type: Date,
require: true,
,
gender:
type: String,
enum: ["Male", "Female"],
required: true,
,
departments:
type: Schema.Types.ObjectId,
required: true,
ref: "Department",
,
,
timestamps: true ,
);
const doctor = mongoose.model("doctor", doctorSchema);
module.exports = doctor;
我的 Ejs 表单
<form action="/doctors/newdoctor" method="post" class="addFormBody">
<div class="row">
<div class="col">
<label for="" class="form-label">Full Name</label>
<input type="text" name="fullname" class="form-control" placeholder="Full name" aria-label="Full name">
</div>
<div class="col">
<label for="exampleInputEmail1" class="form-label">Email address</label>
<input type="email" name="email" class="form-control" placeholder="Email" aria-label="email">
</div>
</div>
<div class="row">
<div class="col">
<label for="" class="form-label">Date of Birth</label>
<input type="date" name="birth" class="form-control" aria-label="date">
</div>
<div class="col">
<label for="exampleInputEmail1" class="form-label">Gendar</label>
<select name="gender" class="form-select" aria-label="Default select example">
<option selected value=" <%= user.gender%>">Male </option>
<option selected value=" <%= user.gender %>"> Female </option>
</select>
</div>
</div>
<div class="row">
<div class="col">
<label for="" class="form-label">Profile Image</label>
<input type="file" name="profileimg" class="form-control" aria-label="date">
</div>
<div class="col">
<label for="exampleInputEmail1" class="form-label">Department</label>
<select name="department" class="form-select" aria-label="Default select example">
<% departments.forEach(departments => %>
<option selected value="<%= departments.id %> "> <%= departments.name %> </option>
<% ) %>
</select>
</div>
</div>
<button class="btn btn-secondary"> Submit</button>
</div>
</form>
还有我的控制器
const express = require("express");
const department = require("../models/department");
const doctors = require("../models/doctors");
const router = express.Router();
router.get("/", async (req, res) =>
res.render("doctor", username: req.session.fullname );
);
router.get("/newdoctor", async (req, res) =>
const departments = await department.find();
const user = await doctors.find();
console.log(req.body);
res.render("add-doctor",
username: req.session.fullname,
departments,
user,
);
);
router.post("/newdoctor", (req, res) =>
console.log(req.body);
);
module.exports = router;
【问题讨论】:
您是否遇到任何错误?您是否尝试过使用<% console.log(departments) %>
来查看是否实际定义了departments
?
@Tyler2P 我的部门有错误,说它未定义,请问我该如何解决这个问题?
【参考方案1】:
你检查用户的数据格式了吗?
https://mongoosejs.com/docs/api.html#model_Model.find 返回一个查询结构,因此问题可能与用户的数据结构有关。
【讨论】:
以上是关于使用 Express、Mongodb 和 EJ 显示下拉动态数据的主要内容,如果未能解决你的问题,请参考以下文章
重点突破—— Nodejs+Express+MongoDB的使用基础
Node.JS、Express 和 MongoDB :: 多个集合
使用 Nodejs、Express、Mongoose 和 React 将图像上传到 MongoDB
使用node+express+mongodb实现用户注册登录和验证功能