Ваше текущее приложение несколько не в порядке, вам нужно сначала сохранить документ домена, и при успешном сохранении вы можете создать документ задачи.
Попробуйте:
public addNewTask (req: Request, res:Response){
// create the domain document first, before creating the task document, and then store its _id in the task document
let _domain = new domain(req.body._domain);
_domain.save((err,_domain)=>{
let newTask = new Task();
newTask.user = req.body.user;
newTask.project = req.body.project;
newTask.issue = req.body.issue;
newTask.title = req.body.title;
newTask.dateCreated = req.body.dateCreated;
// here you only need to store the _id of the newly created _domain document
newTask.records = [{
_domain : _domain._id,
time : req.body.time
}]
newTask.save((err, task)=>{
if(err){
res.send(err);
}
//if you want populated _domain object in your records array, you can use .populate()
Task.populate(task,{path : records._domain},(err,task) =>
{
res.json(task);
})
});
})
}
Я предполагаюВаше тело запроса выглядит так:
{
user : "user_name",
project : "project_name",
issue : "issue_name",
title : "title_",
dateCreated : "date",
_domain : {
label : "some_label"
},
time : 12345
}