【发布时间】:2015-12-27 17:22:34
【问题描述】:
我正在使用平均堆栈(mongo/mongoose、ember、angular、node)。
数据库包含 2 个模式
var ChildSchema = new Schema({
childName: {
type: String,
required: true,
trim: true
}
});
var ParentSchema = new Schema({
parentName: {
type: String,
required: true,
trim: true
},
child: {
type: Schema.ObjectId,
ref: 'Child'
}
});
mongoose.model('Parent', ParentSchema);
mongoose.model('Child', ChildSchema);
有一个 html form 允许用户创建一个新的Parent,该表单有一个选择框,其中填充了存储在数据库中并由findChildren() 函数检索的子项。
HTML 在这里:
<section data-ng-controller="ParentController" data-ng-init="findChildren()" >
<form name="parentForm" class="form-horizontal col-md-6" role="form" data-ng-submit="create(parentForm.$valid)" novalidate>
<div class="form-group" ng-class="{ 'has-error' : submitted && parentForm.config.$invalid }">
<label for="child" class="col-md-2 control-label">child</label>
<div class="col-md-9">
<select ng-model="parent.child" ng-options="child as child.childName for child in childs"></select>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<button mean-token="'create-submit'" type="submit" class="btn btn-info">Submit</button>
</div>
</div>
</form>
</section>
html form 将孩子的名字显示为列表中的可选项目,当用户按下提交按钮时,节点应用程序中的控制器会收到正确的父对象 ID。这是控制器代码
create: function(req, res) {
var parent = new Parent(req.body);
parent.save(function(err) {
if (err) {
return res.status(500).json({
error: 'Cannot save the parent'
});
}
res.json(parent);
});
}
问题是我在尝试在表格中显示父母列表时无法显示 childName。这是HTML。显示所有父母的其他信息
<section data-ng-controller="DevicesController" data-ng-init="findParents()">
<table border="1" class="mytable">
<tr>
<td>oject</td>
<td>Parent Name</td>
<td>Child</td>
</tr>
<tr ng-repeat="parent in parents">
<td>{{parent}}</td>
<td>{{parent.parentName}}</td>
<td>{{parent.child.childName}}</td>
</tr>
</table>
</section>
期望 parent.child.childName 将显示被引用对象的 childName。它返回一个空值。如果我显示'parent.child',它将显示objectID
例如:
我有一个孩子叫
ChildA。当我查看创建父级的 html 表单时,我将输入 ParentA 作为名称并从列表中选择 ChildA。
当我按下提交时,新的父对象将被创建。
当我查看父列表时,显示的表格应显示一行带有
ParentA和ChildA。现在它显示
ParentA和一个空白
【问题讨论】: