I'm beginning to use restangular for the first time and struggling to access nested documents. If I have a Mongoose schema like so:
var mongoose = require('mongoose');
module.exports = function Schemas() {
this.schema = mongoose.Schema;
this.EmployeeSchema = new this.schema({
'firstname': {
type: String,
required: true,
trim: true
},
'lastname': {
type: String,
required: true,
trim: true
},
'email': {
type: String,
required: true,
trim: true,
index: true,
validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
},
'departmentId': {
type: this.schema.ObjectId,
trim: true,
required: true
},
'enddate': {
type: String,
trim: true
},
'active': {
type: Boolean,
"default": true
}
});
this.EmployeeSchemaModel = mongoose.model('employees', this.EmployeeSchema);
this.DepartmentSchema = new this.schema({
'name': {
type: String,
required: true,
trim: true,
index: {
unique: true
}
},
'employees': [this.EmployeeSchema]
});
this.DepartmentSchemaModel = mongoose.model('departments', this.DepartmentSchema);
mongoose.connect('mongodb://localhost:8120/staff');
};
To access and employee within a department, I thought I could simply do the following:
var getEmployeeById = function(departmentId, empId) {
Restangular.one('departments',departmentId).one('employees', empId).get().then(function (emp){
return emp;
});
Although I can retrieve a department, and when I do, it has a nested employees array, correctly populated with the employees I expect. I can't actually retrieve an individual employee via its id as I intended. I end up with a 404.
What am I doing wrong?