title: dynamic reference in mongodb not displaying username
body: <p>So I have an application where users can create accounts either local or with twitter and can create posts. I want the users name to be displayed on the post whenever they make one.</p>
  <p>So far I have 3 schemas, LocalUser, TwitterUser and Posts. I have looked at mongoose documentation (<a href="https://mongoosejs.com/docs/populate.html#dynamic-ref" rel="nofollow noreferrer">mongoose docs</a>) which allows you to dynamically reference schemas. However, this is not working for me.</p>
  <p>models/Post.js</p>
  <pre><code>const PostsSchema = new mongoose.Schema({
   postTitle: {
     type: String,
     required: true,
     trim: true,
   },
   user: {
     // Connects user to posts
     type: mongoose.Schema.Types.ObjectId,
     refPath: 'onModel',
   },
   onModel: {
     type: String,
     // required: true -&gt; if I put this I get an error on post
     enum: ['LocalUser', 'TwitterUser'],
   },
  });
  
  module.exports = mongoose.model('Posts', PostsSchema);
  
  </code></pre>
  <p>index.js</p>
  <pre><code>router.get('/', async (req, res) =&gt; {
    try {
      const posts = await Posts.find()
        .populate('onModel')
        .lean();
  
      const { user: { username, image, twitterId, googleId } = {} } = req;
      res.render('index', {
        username,
        twitterId,
        googleId,
        image,
        posts,
      });
    } catch (err) {
      console.error(err);
      res.render('error/500');
    }
  });
  
  </code></pre>
  <p>index.hbs</p>
  <pre><code>{{onModel.username}} // nothing - Note, twitter schema and local schema both have username
  </code></pre>
  <p>I have been looking at multiple solutions and this one looked the most promising. Am I missing something? Thanks.</p>