我正在构建业务管理软件,我正在创建一个Jobs集合,并将单个Employee和Customer对象及其所有数据从他们自己的集合(Employee& Customer)链接到Jobs集合。
然后我在表上加载Jobs集合中的所有对象(单个作业)。并继续收到错误说“Job.aggregate(...)。toArray不是函数”
这是当前的获取请求
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});
}
});
});
以下是Employee Schema和Jobs Schema我想将所有数据链接到每个Jobs对象
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);
如何重写此功能,以便在我的ejs页面加载对象时加载
<% Jobs.forEach(function(Jobs){ %>
<%= Jobs.jobCustomer.first_name %>
<%= Jobs.jobEmployee.first_name %>
<% }) %>
这是我的工作计划
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);
当我创建一个新作业并添加到集合时,它从所选客户和员工获取objectId现在我需要帮助显示具有相同objectId的客户和员工的信息