请帮助我一下这里我想创建一个包含车辆的报告,但是,我的结果是按预期进行的。
拳头文件在
之下var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var reportSchema = new Schema({
reportDate : { type: Date, default: Date.now},
description: { type: String, required: true},
address: String,
vehicule : [{type: String, ref: 'Vehicule'}],
user : [{type: String, ref: 'User'}]
})
var Vehicule = mongoose.model('Report', ReportSchema);
module.exports = Report;
报告的第二个架构如下所示,但是当我创建报告时,它不包括报告中的vehicule ID。车辆以空阵列的形式返回。任何帮助将不胜感激
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var VehiculeSchema = new Schema({
make: String,
model: String,
color:String,
year: String,
vin: {type: String, required:true, unique:true},
imagePath: String,
user : [{type: Schema.ObjectId, ref: 'User'}]
},
{ timestamps: true
});
var Vehicule = mongoose.model('Vehicule', VehiculeSchema);
module.exports = Vehicule;
以下是我用来创建报告的方法
var Vehicule = require('../models/vehicule');
var Report = require('../models/report');
var User = require('../models/user');
exports.newReport = function(req, res, next){
req.body.user = req.user;
req.body.vehicule = req.vehicule;
var report = new Report({description :req.body.description, address: req.body.address, user: req.user, vehicule: req.vehicule});
report.save(function(err, data){
if(err){
return next(err);
res.json({message: "cannot add this report"});
}else {
res.json({message: "report added", data: report})
console.log(JSON.stringify((data)))
}
});
}
以下是我回来的结果,我想知道我错过了什么以及为什么它不起作用。 { “消息”:“报告已添加”, “数据”:{ “__v”:0, “description”:“这是另一个错误”, “地址”:“Quatier vill”, “_id”:“58af4e857ae6770a7d68d85a”, “用户”:[ “{_id:58a7315e0a9f6474a88766fa,角色:'创作者'}” ] “vehicule”:[], “reportDate”:“2017-02-23T21:05:09.384Z” } }