title: Storing and Returning an ObjectId in MongoDB (MEAN Stack)
body: <p>I'm new to the MEAN stack and am having trouble with how to correctly link an ObjectId with MongoDB from my javascript. I am also trying to then get the name that corresponds to that ObjectId to show in my view.</p>
  
  <p>The code is meant to make appointments in a schedule, where users are tied to each appointment, then the view shows information about each appointment (ie. each user's name).</p>
  
  <p>Here is my Schema for the Schedule:</p>
  
  <pre><code>'use strict';
  
  var mongoose = require('mongoose'),
      Schema = mongoose.Schema;
  
  var ScheduleSchema = new Schema({
    description: String,
    category: [String],
    location: String,
    startDate: { type: Date, default: Date.now },
    endDate: { type: Date, default: Date.now },
    participants: [{
      type: Schema.Types.ObjectId,
      ref: 'User'
    }],
    author: {
      type: Schema.Types.ObjectId,
      ref: 'User'
    },
    timestamp: { type: Date, default: Date.now },
    active: Boolean
  });
  
  module.exports = mongoose.model('Schedule', ScheduleSchema);
  </code></pre>
  
  <p>And the Schema for the Users:</p>
  
  <pre><code>var UserSchema = new Schema({
    name: String,
    email: { type: String, lowercase: true },
    role: {
      type: String,
      default: 'user'
    },
    hashedPassword: String,
    provider: String,
    salt: String
  });
  </code></pre>
  
  <p>There's a lot more to that ^ file but I think the schema is the only relevant part.</p>
  
  <p>And here is my controller code that passes fake testing info with 2 real user objectids (participants):</p>
  
  <pre><code>// Use our rest api to post a new meeting
  $scope.addMeeting = function() {
    $http.post('/api/schedules', { description: "Testing schedule adding", participants: ["547f03befccbd4f93874b547", "5481dcbf5dad7f735a766ad9"], category: "1", location: "Small Conference Room", startDate: new Date(), endDate: new Date(), timestamp: new Date(), active: true });
  };
  </code></pre>
  
  <p>I've gotten that to post fine, but when I get all the appointments from the schedule and put them on the view, (maybe obviously) the ObjectId is displayed rather than the name, so this:</p>
  
  <pre><code>&lt;table style="width:100%;"&gt;
    &lt;tr&gt;
      &lt;td class="left" ng-class="'head'"&gt;Time&lt;/td&gt;
      &lt;td ng-class="'head'"&gt;Attendees&lt;/td&gt;
      &lt;td ng-class="'head'"&gt;Description&lt;/td&gt;
      &lt;td class="right" ng-class="'head'"&gt;Location&lt;/td&gt;
    &lt;/tr&gt;
      &lt;tr ng-repeat="meeting in meetings"&gt;
        &lt;td class="left" ng-class-even="'even'"&gt;{{ meeting.startDate | date:"h:mma" }} - {{ meeting.endDate | date:"h:mma" }}&lt;/td&gt;
        &lt;td ng-class-even="'even'"&gt;&lt;span ng-repeat="person in meeting.participants"&gt;{{ person }}&lt;span ng-hide="$last"&gt;, &lt;/span&gt;&lt;/span&gt;&lt;/td&gt;
        &lt;td ng-class-even="'even'"&gt;{{ meeting.description }}&lt;/td&gt;
        &lt;td class="right" ng-class-even="'even'"&gt;{{ meeting.location }}&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Turn into this: (I had an image posted but I don't have enough reputation points, so all the picture showed was the ObjectIds of the Users underneath the 'Participants' column instead of the names)</p>
  
  <p>And I'm wondering how you're supposed to properly link things to be able to show the name instead of those ObjectIds.</p>
  
  <p>Sorry for the novel, hope I put enough to make it clear.</p>