title: Referenced Objects and Virtual getters returning undefined in pug layout file, but queried successfully from database
body: <p>My problem is two-fold, but I think they are both caused by the same issue: my Schema definition and/or Model creation.</p>
  
  <p>I am following <a href="https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs" rel="nofollow noreferrer">this MDN Nodejs tutorial</a> and i cannot figure out:</p>
  
  <ol>
  <li><p>Why my referenced objects are returning undefined in my layout file, yet I have confirmed that the data is being correctly inserted and queried(as far as i can tell).</p></li>
  <li><p>My virtual id getter is not working.</p></li>
  </ol>
  
  <p><strong>Schema Definition:</strong></p>
  
  <p><strong>book.js italize</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  
  const BookSchema = new Schema({
  
    title: {type: String, required: true},
    author: {type: Schema.Types.ObjectId, ref: 'Author', required: true},
    summary: {type: String, required: true},
    isbn: {type:String, required: true},
    genre: [{type: Schema.Types.ObjectId, ref: 'Genre'}]
  
  });
  
  //this doesnt seem to be working. returns undefined on the
  //object in layout file
  BookSchema
  .virtual('url')
  .get(() =&gt; {
    return '/catalog/book/' + this._id;
  });
  
  module.exports = mongoose.model('Book', BookSchema);
  </code></pre>
  
  <p><strong>author.js italize</strong></p>
  
  <pre><code>const mongoose = require('mongoose');
  const Schema = mongoose.Schema;
  
  const AuthorSchema = new Schema({
  
    first_name: {type: String, required: true, max: 100},
    family_name: {type: String, required: true, max: 100},
    date_of_birth: {type: Date},
    date_of_death: {type: Date}
  
  });
  
  //Virtuals
  
  AuthorSchema.virtual('name')
  .get(() =&gt; {
    return this.family_name + ', ' + this.first_name;
  });
  
  //create virtual absolute url to obtain instance of this model
  AuthorSchema.virtual('url')
  .get(() =&gt; {
    return '/catalog/author/' + this._id;
  });
  
  module.exports = mongoose.model('Author', AuthorSchema);
  </code></pre>
  
  <p><strong>Model Creation:</strong></p>
  
  <pre><code>function authorCreate(first_name, family_name, d_birth, d_death, cb) {
    authordetail = {first_name:first_name , family_name: family_name }
    if (d_birth != false) authordetail.date_of_birth = d_birth
    if (d_death != false) authordetail.date_of_death = d_death
  
    var author = new Author(authordetail);
  
    author.save(function (err) {
      if (err) {
        cb(err, null)
        return
      }
      console.log('New Author: ' + author);
      authors.push(author._id)
      cb(null, author)
    }  );
  }
  
  function bookCreate(title, summary, isbn, author, genre, cb) {
    bookdetail = {
      title: title,
      summary: summary,
      author: author,
      isbn: isbn
    }
    if (genre != false) bookdetail.genre = genre
  
    var book = new Book(bookdetail);
    book.save(function (err) {
      if (err) {
        cb(err, null)
        return
      }
      console.log('New Book: ' + book);
      books.push(book._id)
      cb(null, book)
    }  );
  }
  </code></pre>
  
  <p><strong>controller.js:</strong></p>
  
  <pre><code>// Display list of all books.
  exports.book_list = function(req, res) {
  
    Book.find({})
    .populate('author')//this seems to work
    .exec(function (err, list_books) {
      if (err) { return next(err); }
      //Successful, so render
      for(var b in list_books){
        console.log('author id: ' + list_books[b].author._id);//as evidenced here
  
      }
  
      //but the author object is undefined when rendered
      res.render('book_list', { title: 'Book List', book_list: list_books });
  
    });
  
  };
  </code></pre>
  
  <p><strong>layout.pug</strong>:</p>
  
  <pre><code>extends layout
  
  block content
    h1= title
  //url and author undefined here
    ul
      each book in book_list
        li
          a(href=book.url) #{book.title}
          |  #{book.author.name}
  
      else
        li There are no books.
  </code></pre>
  
  <p><strong>Screenshot:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/lm5fO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lm5fO.png" alt="This is the end result"></a></p>
  
  <p>Tools:</p>
  
  <ul>
  <li><p>Node + Express js</p></li>
  <li><p>Pug</p></li>
  <li><p>mLab free tier</p></li>
  </ul>
  
  <p>I am completely new to Mongoose DB &amp; Pug and know only as much as the primers in the tutorial have taught and explicitly pointed to as further reading.</p>
  
  <p>Let me know if you need any more information. Thanks</p>