我试图在我的 REST 服务器的 GET 输出中隐藏某些字段。我有 2 个模式,都有一个字段将彼此的相关数据嵌入到 GET 中,因此获取 /people 将返回他们工作的位置列表,并获取位置列表返回在那里工作的位置。但是,这样做会添加一个 person.locations.employees 字段,然后会再次列出员工,这显然是我不想要的。那么如何在显示之前从输出中删除该字段呢?谢谢大家,如果您需要更多信息,请告诉我。
/********************
/ GET :endpoint
********************/
app.get('/:endpoint', function (req, res) {
var endpoint = req.params.endpoint;
// Select model based on endpoint, otherwise throw err
if( endpoint == 'people' ){
model = PeopleModel.find().populate('locations');
} else if( endpoint == 'locations' ){
model = LocationsModel.find().populate('employees');
} else {
return res.send(404, { erorr: "That resource doesn't exist" });
}
// Display the results
return model.exec(function (err, obj) {
if (!err) {
return res.send(obj);
} else {
return res.send(err);
}
});
});
这是我的 GET 逻辑。所以我一直在尝试在填充函数之后使用猫鼬中的查询函数来尝试过滤掉这些引用。这是我的两个架构。
peopleSchema.js
return new Schema({
first_name: String,
last_name: String,
address: {},
image: String,
job_title: String,
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null },
hourly_wage: Number,
locations: [{ type: Schema.ObjectId, ref: 'Locations' }],
employee_number: Number
}, { collection: 'people' });
locationSchema.js
return new Schema({
title: String,
address: {},
current_manager: String, // Inherit person details
alternate_contact: String, // Inherit person details
hours: {},
employees: [{ type: Schema.ObjectId, ref: 'People' }], // mixin employees that work at this location
created_at: { type: Date, default: Date.now },
active_until: { type: Date, default: null }
}, { collection: 'locations' });