title: TypeError: Cannot read property '_id' of undefined node js
body: <p>I am following the FULL STACK JAVASCRIPT DEVELOPMENT WITH MEAN by Adam Bretz &amp; Colin J. Ihrig. When I got to chapter 8 I could not get past this error when trying to insert into an online mongo database. Do I need to use an older version of node? I've looked through stack exchange and have not been able to make it work following all of the suggested fixes... 
  Here is my error:</p>
  
  <pre><code>TypeError: Cannot read property '_id' of undefined
      at insertEmployees (/Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:100:17)
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:195:5
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/index.js:79:7
      at Function.&lt;anonymous&gt; (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:3369:16)
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:1890:18
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/parallel.js:35:9
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/before.js:31:21
      at iteratorCallback (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/eachOf.js:52:13)
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/onlyOnce.js:12:16
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/internal/parallel.js:32:13
      at apply (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/_apply.js:15:25)
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/async/node_modules/lodash/_overRest.js:32:12
      at model.callbackWrapper (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:1865:11)
      at next_ (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:89:34)
      at fnWrapper (/Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/node_modules/hooks-fixed/hooks.js:186:8)
      at /Users/kikocarisse/Desktop/NODESITES/chapter8/node_modules/mongoose/lib/model.js:3369:16
  </code></pre>
  
  <p>and here is my code:</p>
  
  <pre><code>"use strict";
  var a = require('./a.js');
  var b = new a(5);
  var mongoose = require('./node_modules/mongoose');
  var express = require('express');
  var app = express();
  
  var bodyParser = require('./node_modules/body-parser');
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.route('/echo')
      .all((req,res)=&gt;{
          let pars = (Object.keys(req.body).length &gt; 0)?req.body:req.query;
          res.send(pars);
      });
  
  
  var Schema = mongoose.Schema;
  var db = mongoose.connection;
  var dbUrl = 'mongodb://*****:******@******:*****/****';
  
  var TeamSchema = new Schema({
    name: {
      type: String,
      required: true
    }
  });
  var Team = mongoose.model('Team', TeamSchema);
  
  var EmployeeSchema = new Schema({
    name: {
      first: {
        type: String,
        required: true
      },
      last: {
        type: String,
        required: true
      }
    },
    team: {
      type: Schema.Types.ObjectId,
      ref: 'Team'
    },
    image: {
      type: String,
      default: 'images/user.png'
    },
    address: {
      lines: {
        type: [String]
      },
      postal: {
        type: String
      }
    }
  });
  
  var Employee = mongoose.model('Employee', EmployeeSchema);
  
  
  db.on('error', function () {
    console.log('there was an error communicating with the database');
  });
  
  function insertTeams (callback) {
    Team.create([{
      name: 'Product Development'
    }, {
      name: 'Dev Ops'
    }, {
      name: 'Accounting'
    }], function (error, pd, devops, acct) {
      if (error) {
        return callback(error);
      } else {
        console.info('teams successfully added')
        callback(null, pd, devops, acct);
      }
    });
  }
  
  function insertEmployees (pd, devops, acct, callback) {
    Employee.create([{
      name: {
        first: 'John',
        last: 'Adams'
      },
      team: pd._id,
      address: {
        lines: ['2 Lincoln Memorial Cir NW'],
        postal: '20037'
      }
    }, {
      name: {
        first: 'Thomas',
        last: 'Jefferson'
      },
      team: devops._id,
      address: {
        lines: ['1600 Pennsylvania Avenue', 'White House'],
        postal: '20500'
      }
    }, {
      name: {
        first: 'James',
        last: 'Madison'
      },
      team: acct._id,
      address: {
        lines: ['2 15th St NW', 'PO Box 8675309'],
        postal: '20007'
      }
    }, {
      name: {
        first: 'James',
        last: 'Monroe'
      },
      team: acct._id,
      address: {
        lines: ['1850 West Basin Dr SW', 'Suite 210'],
        postal: '20242'
      }
    }], function (error, johnadams) {
      if (error) {
        return callback(error);
      } else {
        console.info('employees successfully added');
        callback(null, {
          team: pd,
          employee: johnadams
        });
      }
    })
  }
  
  function retrieveEmployee (data, callback) {
    Employee.findOne({
      _id: data.employee._id
    }).populate('team').exec(function (error, result) {
      if (error) {
        return callback (error);
      } else {
        console.log('*** Single Employee Result ***');
        console.dir(result);
        callback(null, data);
      }
    });
  }
  
  function retrieveEmployees (data, callback) {
    Employee.find({
      'name.first': /J/i
    }, function (error, results) {
      if (error) {
        return callback(error);
      } else {
        console.log('*** Multiple Employees Result ***')
        console.dir(results);
        callback(null, data);
      }
    });
  }
  
  function updateEmployee (first, last, data, callback) {
    console.log('*** Changing names ***');
    console.dir(data.employee);
  
    var employee = data.employee;
    employee.name.first = first;
    employee.name.last = last
  
    employee.save(function (error, result) {
      if (error) {
        return callback(error);
      } else {
        console.log('*** Changed name to Andrew Jackson ***');
        console.log(result);
        callback(null, data);
      }
    });
  }
  
  mongoose.connect(dbUrl, function (err) {
    if (err) {
      return console.log('there was a problem connecting to the database!' + err);
    }
    console.log('connected!');
  
    insertTeams(function (err, pd, devops, acct) {
      if (err) {
        return console.log(err)
      }
      insertEmployees(pd, devops, acct, function (err, result) {
  
        retrieveEmployee(result, function (err, result) {
  
          retrieveEmployees(result, function (err, result) {
  
            updateEmployee('Andrew', 'Jackson', result, function (err, result) {
              if (err) {
                console.error(err);
              } else {
                console.info('database activity complete')
              }
  
              db.close();
              process.exit();
            });
          });
        });
      });
    });
  });
  </code></pre>