0

I am creating a Business Management platform, and have been trying to figure out how to write this one solution for days now. Right now I have an EmployeeSchema, CustomerSchema, and a JobsSchema. In the Job Schema I am saving the assigned Customer and Employee Object Id, so I can load the Jobs on a table and it will show the name of the Customer and Employee that is assigned the Job. I read on how to use .insert and tried to write it into my code and it says TypeError: Job.find(...).forEach is not a function, is what I am writing correct. Also I know you can use $lookup (aggregation) mongo offers and I would like to use that if this doesn't work, I just do not know how to write it with what I have.

This is what I have right now when I load the Jobs Table Page

app.js GET Request

router.get("/upcoming-jobs", isLoggedIn, function(req,res){

Job.find({author: req.user._id}).forEach(function(newJob){
 newJob.employee = Employee.findOne({"_id": Job.employee}).toArray();
 newJob.customer = Customer.findOne({"_id": Job.customer}).toArray();
 Job.newestJob.insert(newJob);
 res.render("upcoming-jobs", {Jobs: newJob});
}, function(err){
 console.log(err);
});
});

EMPLOYEE SCHEMA

var mongoose = require("mongoose");
var EmployeeSchema = new mongoose.Schema({
//ALL EMPLOYEE DATA NEEDED
  first_name: String,
  last_name: String,
  phone_number: String,
  email: String,
  address: String,
  city: String,
  state: String,
  zipcode: String,
  clocked_in: false,
  emergency_contact_name: String,
  emergency_contact_number: String,
  schedule: [{"date":String, "time":String}],
  hours_worked: [{"date":String, "time":String}],
  author: {type: mongoose.Schema.Types.ObjectId, ref:"User"},
  submittedBy: String
});

module.exports = mongoose.model("Employees", EmployeeSchema);

CUSTOMER SCHEMA

var mongoose = require("mongoose");

var CustomerSchema = new mongoose.Schema({
  // all customer data
  first_name: String,
  last_name: String,
  phone_number: String,
  email: String,
  address: String,
  city: String,
  state: String,
  zipcode: String,
  notes: String,
  author: {type: mongoose.Schema.Types.ObjectId, ref:"User"},
  submittedBy: String
});

module.exports = mongoose.model("Customers", CustomerSchema);

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,
  submittedBy: String,
  start_time: String,
  job_duration: {clocked_in: String, clocked_out: String},
  notes: String
});

module.exports = mongoose.model("Jobs", JobsSchema);

This is the Jobs Table EJS page, that I am rendering this data on

                                    <table id="myTable" class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>Name</th>
                                            <th>Address</th>
                                            <th>Time</th>
                                            <th>Employee</th>
                                            <th>Select</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                      <% Jobs.forEach(function(Jobs){ %>
                                        <tr>
                                            <td><%= Customers.first_name + " " + Jobs.customer %></td>
                                            <td><%= Jobs.submittedBy %></td>
                                            <td>1:00pm</td>

                                            <td><%= Employee.first_name + " " + Jobs.employee %></td>

                                            <td><button class="btn btn-success">Select</button></td>
                                        </tr>
                                        <% }) %>
                                    </tbody>

This page will insert the ObjectID of the Jobs.customer & Jobs.employee but will not load the CustomersSchema or EmployeeSchema first_name.

If I wanted to use $lookup instead how would I write that? I am pretty knew to nodejs and programming in general and need some help, Thank you.

  • If I was to use the $lookup or .insert method how would I write it with what I have so far? – Brandon Priest May 18 '18 at 00:24
  • I updated this post with a new problem and I added a .insert function I tried to write in my application, would love to get some help – Brandon Priest May 18 '18 at 01:05
  • I would use a library such as Mongoose and rather use populate than doing a lookup in your case. Also don't forget that find() is asynchronous and should be used like this: Jobs.find({}, (err, jobs)=> res.json(jobs)) // or anything you are willing to achieve. – Eld0w May 19 '18 at 01:44

0 Answers0