我有2个模型员工和任务列表。
var mongoose=require("mongoose");
var findOrCreate = require('mongoose-findorcreate');
var uniqueValidator = require('mongoose-unique-validator');
var employeeSchema = new mongoose.Schema({
empId : { type : Number , unique : true, required : true },
empName : { type : String , unique : true, required : true },
empTeam : { type : String , required : true },
created : {type : Date, default : Date.now}
});
employeeSchema.plugin(findOrCreate,uniqueValidator);
module.exports = mongoose.model("Employee", employeeSchema);
// Mongoose Tasklist Model config
var mongoose=require("mongoose");
var findOrCreate = require('mongoose-findorcreate');
var uniqueValidator = require('mongoose-unique-validator');
var tasklistSchema = new mongoose.Schema({
taskId : { type : Number , unique : true, required : true },
taskDesc : { type : String , unique : true, required : true },
taskDetails : {type : String},
taskAssignee : [{
type:mongoose.Schema.Types.ObjectId,
ref:"Employee"
}],
startDate: Date,
dueDate: Date,
taskStatus: String,
created : {type : Date, default : Date.now},
});
tasklistSchema.plugin(findOrCreate, uniqueValidator);
module.exports = mongoose.model("Tasklist", tasklistSchema);
我有一个编辑路由,其中我为任务分配了多个受托人。
router.post("/tasklist/:id/edit", function(req,res){
Tasklist.findOneAndUpdate({_id:req.params.id},req.body.tasklist, async function(err, tasklist){
if(err){
req.flash("error", "Unable to update the tasklist record : "+err.message);
res.redirect("/employee");
} else {
var emp_id=req.body.taskAssignee.split(',');
我能够将员工推送到 tasklist.taskAssignee,还可以执行 tasklist.save()。没有错误。但是,更新不会保留在 mongoDB 中。
慕尼黑5688855
相关分类