I am building a Business Management Software, and I am creating a Jobs collection and linking a single Employee and Customer object and all their data from their own individual collection (Employee & Customer) to the Jobs Collection.
I am then loading all the Objects(Individual Jobs) from Jobs collection on a table. And continue to get an error saying " Job.aggregate(...).toArray is not a function "
This is the current get request
router.get("/upcoming-jobs", isLoggedIn, function(req, res){
Job.find({author: req.user._id}, function(err, allJobs){
if(err){
console.log(err);
} else {
Job.aggregate([
{ $lookup: {
from: 'Customers',
localField: 'customer',
foreignField: '_id',
as: 'jobCustomer'}
},
{ $unwind: "$jobCustomer" },
{ $lookup: {
from: "Employees",
localField: "employee",
foreignField: '_id',
as: 'jobEmployee'}
},
{ $unwind: "$jobEmployee"},
]).toArray(function(err, res){
if(err){
console.log(err);
} else {
console.log(res);
}
});
});
res.render("upcoming-jobs", {Jobs: allJobs});
}
});
});
Here is the Employee Schema and Jobs Schema I would like to link all the data into each Jobs Object
var EmployeeSchema = new mongoose.Schema({
//ALL EMPLOYEE DATA NEEDED
first_name: String,
last_name: String,
phone_number: String,
email: String,
address: String,
author: {type: mongoose.Schema.Types.ObjectId, ref:"User"},
submittedBy: String
});
module.exports = mongoose.model("Employees", EmployeeSchema);
CUSTOMER SCHEMA
var CustomerSchema = new mongoose.Schema({
// all customer data
first_name: String,
last_name: String,
phone_number: String,
email: String,
address: String,
author: {type: mongoose.Schema.Types.ObjectId, ref:"User"},
submittedBy: String
});
module.exports = mongoose.model("Customers", CustomerSchema);
How can I rewrite this to work so when I load the object on my ejs page it will load
<% Jobs.forEach(function(Jobs){ %>
<%= Jobs.jobCustomer.first_name %>
<%= Jobs.jobEmployee.first_name %>
<% }) %>
HERE IS MY JOBS SCHEMA
var mongoose = require("mongoose");
var JobsSchema = new mongoose.Schema({
customer: {type: mongoose.Schema.Types.ObjectId, ref:"Customers"},
employee: {type: mongoose.Schema.Types.ObjectId, ref:"Employees"},
author: {type: mongoose.Schema.Types.ObjectId, ref:"User"},
service: String,
});
module.exports = mongoose.model("Jobs", JobsSchema);
When I create a new Job and add to collection it gets the objectId from the selected customer and employee now I need help displaying the information of the customer and employee with the equal objectId