【发布时间】:2020-08-05 03:05:08
【问题描述】:
所以我有一个包含名称和部门的社区模式,部门字段有一个类型数组..如何根据名称字段在下拉列表中呈现部门。
这是我的 routes.js
router.get('/setup', (req, res,next) => {
Community.find(function(err, data) {
res.render('setup', {
community: data,
});
});
});
这是我的社区架构
const CommunitySchema = new mongoose.Schema({
name: {
type: String
},
department: [{
type: String
}]
});
这是我的 setup.ejs
<label for="fullname"><strong>What department do you belong to ?</strong></label>
<select class="form-control" id="community" name="community">
<optgroup label="Select Table">
<% community.forEach(function (practice) { %>
<option > <%= practice.department.pop() %> </option>
<% }) %>>
</optgroup>
</select>
**这是数据**
{
"name":"Engineering",
"departments":[ "Electrical engineering", "Mechanical engineering", "Chemical engineering" ]
},
{
"name":"Arts",
"departments":[ "Philosophy", "Theatre arts", "English" ]
}
我正在尝试制作一个依赖下拉列表,希望我的问题被理解
【问题讨论】:
-
您能否在
find查询之后添加数据的结构方式.....只需console.log(data)并在可能的情况下在此处添加示例数据 -
@RohitAmbre 我已经添加了示例数据。谢谢
标签: javascript node.js mongodb express ejs