angularjs - can not retrieve property of object from an objectID passed by html form -
i using mean stack (mongo/mongoose, ember, angular, node).
the database includes 2 schemas
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); there html form allows user create new parent, form has select box populated children stored in database , retrieved findchildren() function.
the html here:
<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 child.childname 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> the html form shows children's names selectable items in list , when user presses submit button, controller in node application receives correct parent object id. here controller code
create: function(req, res) { var parent = new parent(req.body); parent.save(function(err) { if (err) { return res.status(500).json({ error: 'cannot save parent' }); } res.json(parent); }); } the issue not able display childname when trying show list of parents in table. here html. of parents other information shown
<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> the expectation parent.child.childname display childname of referenced object. returns null. if display 'parent.child', display objectid
for example:
i have child called
childa.when view html form create parent, enter parenta name , select childa list.
when press submit, new parent object created.
when view parent list, table shown should show row
parenta,childa.right shows
parenta, blank
parent.child return objectid correct, need use populate if want details of reference.
i don't see relavent code findparents, below:
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 }); } });
Comments
Post a Comment