【问题标题】:can not retrieve property of object from an objectID passed by html form无法从 html 表单传递的 objectID 中检索对象的属性
【发布时间】: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。

当我按下提交时,新的父对象将被创建。

当我查看父列表时,显示的表格应显示一行带有ParentAChildA

现在它显示 ParentA 和一个空白

【问题讨论】:

    标签: angularjs mongoose mean


    【解决方案1】:

    parent.child 将返回正确的objectid,如果您想获取参考的详细信息,则需要使用populate

    我没有看到findParents 的相关代码,您基本上会执行以下操作:

        child.findOne({"_id": req.params.id}).populate('parent').exec(function (err, child) {
                    if (err) {
                        res.status(403).json({
                            "status": "403",
                            "data": "Forbidden"
                        });
                    } else {
                        res.status(200).json({
                            "status": "200",
                            "message": "Success",
                            "data" : child
                        });
                    }
       });
    

    【讨论】:

    • 啊。那是一个新手问题。谢谢@Thalaivar。我没有正确使用填充功能。修复后,一切正常!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多