我正在制作一个用于存储包含工作帖的练习的api。当我开始练习时,我得到了工作岗位数组,但其中仅包含id并在属性上创建/更新
这是我的工作帖子架构
'use strict';
// Requirements
const mongoose = require('mongoose');
// Workpost Schema
const WorkpostSchema = new mongoose.Schema({
workpost_name: { type: String},
workpost_icon: { type: String},
workpost_info: { type: String},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now},
},{versionKey: false});
// Export the Workpost schema
let Workpost = mongoose.model('Workpost', WorkpostSchema);
module.exports.WorkpostSchema = WorkpostSchema;
module.exports.Workpost = Workpost;
这是使用它的练习模式
'use strict';
// Requirements
const mongoose = require('mongoose');
const WorkScheduleSchema = require('./workschedule-module').WorkScheduleSchema;
const WorkpostSchema = require('./workpost-module').WorkpostSchema;
// Practice Schema
const PracticeSchema = new mongoose.Schema({
practice_name: {type: String, required: true},
address: String,
email: String,
tel: String,
workschedules: [WorkScheduleSchema],
workposts: [WorkpostSchema],
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now},
},{versionKey: false});
// Export the Practice schema
let Practice = mongoose.model('Practice', PracticeSchema);
module.exports.Practice = Practice;
当我尝试练习时,会得到以下提示:
address: "Some adress"
created_at: "2019-03-25T12:42:05.288Z"
email: "email@email.com"
practice_name: "Some practice"
tel: "00000473975832"
updated_at: "2019-03-25T12:42:05.288Z"
workposts: Array(2)
0:
created_at: "2019-03-25T12:42:05.291Z"
updated_at: "2019-03-25T12:42:05.291Z"
_id: "5c94f6eb1a0a6200128461d2"
__proto__: Object
1:
created_at: "2019-03-25T12:42:05.290Z"
updated_at: "2019-03-25T12:42:05.290Z"
_id: "5c94f7121a0a6200128461d3"
返回中不包含其他任何属性。我使用以下代码进行练习,并通过简单的get请求将其返回
router.post('/', (req, res, next) => {
WorkPosts.find({_id: {$in: req.body.workposts}}, {_id:1}).exec( function (err, workposts) {
if (err) {
err.message = "Workposts not found";
return next(err);
}
let practice = new Practice(req.body);
practice.workschedules = workschedules;
practice.workposts = workposts;
practice.save((err, practice) => {
if (err) {
err.message = "Practice couldn't be saved";
return next(err);
}
res.statusCode = 201;
res.json(practice);
});
});
});