你好在nodejs中工作,并且想要保存只有分配的用户才能完成的任务,这种情况是将重复任务分配给一群人,他们必须自己完成任务,
问题:“当用户完成任务然后每个人都完成任务时,我只需要登录的用户完成任务,那么只有他的任务就可以完成任务,而不是每个人的任务”
我只是使用下面的代码对其进行修复,但是它无法正常工作,并说“ SyntaxError:意外令牌。req.user._id= task.id”
代码:
Task.findOne({
req.user._id : task.id,
});
完整功能:
var completeTask = function(req, res, next) {
// return res.status(200).send(req.body);
req.assert("comment", "Please enter the comment.").notEmpty();
const taskId = req.body.taskId || req.params.id;
console.log(taskId);
const [id, recurringTaskNo] = taskId.split(",");
if (!req.validationErrors()) {
Task.findOne({
_id: mongoose.Types.ObjectId(id)
}).exec(function(err, task) {
if (err) {
next(err);
return;
}
if (!task) {
return res.status(404).send("Task not found!");
// if (req.params.home) {
// res.redirect("/");
// } else {
// res.redirect(req.originalUrl);
// }
// return;
}
task.comment = req.body.comment;
if (task.isRecurring) {
const recurringTask = task.recurringTask[recurringTaskNo];
recurringTask.note = req.body.note;
recurringTask.completedAt = new Date();
} else {
task.note = req.body.note;
task.status = "completed";
}
if (req.files && req.files.length > 0) {
async.eachSeries(
req.files,
function(file, cbFile) {
saveFileDetails(file, function(err, fileMetaId) {
if (err) {
cbFile(err);
return;
}
task.attachments.push(fileMetaId);
cbFile();
});
},
function(err) {
if (err) {
next(err);
return;
}
task.save(function(err) {
if (err) {
next(err);
return;
}
req.flash("info", "Successfully completed the task.");
if (req.params.home) {
res.redirect("/");
} else {
res.redirect(req.originalUrl);
}
});
}
);
} else {
task.save(function(err, doc) {
if (err) {
next(err);
return;
}
req.flash("info", "Successfully completed the task.");
/*if (doc.isRecurring) {
const recurringTask = doc.recurringTask[recurringTaskNo];
let preparedTask = Object.assign({}, doc.toJSON(), {
dueDate: recurringTask.date,
note: recurringTask.note,
status: recurringTask.completedAt ? "completed" : "pending"
});
res.status(200).send(preparedTask);
} else*/
res.status(200).send(doc);
// if (req.params.home) {
// res.redirect("/");
// } else {
// res.redirect(req.originalUrl);
// }
});
}
});
} else {
req.flash("error", req.validationErrors());
res.redirect(req.originalUrl);
}
Task.findOne({
req.user._id : task.id,
school: req.session.currentSchool._id
});
};
db:
groupTo: [{ type: String }],
您以字符串格式获取ID,因此您需要将其转换为对象ID才能进行转换。How to convert string into ObjectId看看是否有帮助?