0

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

  • Because it's **mongoose** and not the core driver. Mongoose returns an array by default. You also cannot loop async calls like that. Instead, show some data and the expected result. Also keep the question in parts as asking how to query and render +other things becomes far too broad a question. [Edit your question](https://stackoverflow.com/posts/50441737/edit) and keep it relevant "to the query results expected". We can answer that. Once you have an "array of results" then the basic question of how to render that has many existing answers. Show relevant documents from the MongoDB collection. – Neil Lunn May 21 '18 at 03:50
  • When i get rid of the loop, it continues to call the error Job.aggregate()...toArray is not a function, why is that? – Brandon Priest May 21 '18 at 03:59
  • You're doing a number of things wrong here. "Show the documents" from your MongoDB collection for `Jobs`, `Customers` and `Employee` that should relate to each other and are relevant to your "author" request. Then show what you expect as a result. Looping is not doing anything for you here as you are simply repeating the aggregate operation anyway. Instead we want to "merge" to a single operation here. But we cannot show you without the documents. And then give you an understanding of how to return the response, which is the basic problem you are still missing on top of all of that. – Neil Lunn May 21 '18 at 04:03
  • Okay I edited the question without the loop and added the Jobs Schema as well – Brandon Priest May 21 '18 at 04:40
  • **"Show your documents"**. Open a `mongo` shell, find and copy the content of the "documents". The schema does not really tell us much as we need to see the actual data. This is all part of [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). When you ask a question here, we are not sitting at your desk. In order to "reproduce" we require some data to "reproduce" from. Let's see if you can actually get one answer to all the questions you have posted here so far. Read what I have said and give us some data please. – Neil Lunn May 21 '18 at 04:43

0 Answers0