Looking to make adjustments to my Api in order to populate a referenced schema. Here's the schema I am working with:
export const taskSchema = new Schema ({
user:{
type: String,
required: true
},
project: {
type: String,
required: true
},
issue: {
type: String,
required: true
},
title: {
type: String,
required: true
},
records : [{
_domain: {
type: Schema.Types.ObjectId,
ref: 'TaskDomains'
},
time: {
type:Number
}
}],
dateCreated: {
type: Date,
default: Date.now
}
});
Now, let me show you the taskDomain schema :
export const TaskDomains = new Schema ({
label:{
type: String,
required: true
}
});
I'm struggling with editing the following post method to populate the referenced TaskDomain schema. Take a look at the method below:
import * as mongoose from 'mongoose';
import {taskSchema,TaskDomains} from '../models/tasks.model';
import {Request, Response} from 'express';
const Task = mongoose.model('Task', taskSchema);
const domain = mongoose.model('domain', TaskDomains);
export class taskController{
public addNewTask (req: Request, res:Response){
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;
newTask.records = new domain(req.body._domain);
newTask.records = new domain(req.body._domain.label);
newTask.records = req.body.time;
newTask.save((err, task)=>{
if(err){
res.send(err);
}
res.json(task);
});
}
}