我第一次开始使用 restangular 并努力访问嵌套文档。如果我有这样的猫鼬模式:
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');
};
要访问部门内的员工,我想我可以简单地执行以下操作:
var getEmployeeById = function(departmentId, empId) {
Restangular.one('departments',departmentId).one('employees', empId).get().then(function (emp){
return emp;
});
虽然我可以检索一个部门,并且当我这样做时,它有一个嵌套的员工数组,正确地填充了我期望的员工。我实际上无法按照我的意图通过其 id 检索单个员工。我最终得到一个404。
我究竟做错了什么?