关于使用populate with mongoose和prefilling数据感到困惑

时间:2015-05-03 20:09:52

标签: javascript angularjs node.js mongodb mongoose

我是一个菜鸟并创建了一个收藏 - 约会 - 其中有一个名为staff的字段。

员工是一个单独的集合,并且有一个id。

我试图显示所有约会的列表,并创建一个选择下拉列表,这样我就可以更改工作人员。

我很困惑,因为我只将Appointment.staff设置为该特定职员的staff._id,但我希望选择下拉菜单显示他们的名字。

我想我需要使用'填充',但是因为我是菜鸟,我不确定。

我该怎么做?

这是约会列表的控制器

angular.module('MyApp')
.controller('adminController', ['$scope', 'Appointment', '$state', '$rootScope' , '$stateParams', '$log', '$modal', 'Staff',  function($scope, Appointment, $state, $rootScope, $stateParams, $log, $modal, Staff) {

$scope.apps  = Appointment.query();
$scope.staffList = Staff.query();

}])

下拉列表的代码:

 <td>
      <select  class="form-control" name="staff" 
             ng-options="staff._id as staff.name for staff in staffList" 
             ng-model="app.staff" 
             ng-change="selectChange(app)"
                            >
         <option></option>
      </select>
</td>

以及约会的模式

var mongoose = require('mongoose');

//Creates the appointments Schema for the User details

var appointmentSchema = new mongoose.Schema({
  name: String, 
  email: { type: String, unique: true },
  address_1: String,
  address_2: String,
  city: String,
  postcode: String,
  phone: String,
  appointment_date: String, 
  token: String, 
  StripeCustomerId: String, 
  total_time: Number,
  total_charge: Number,
  lat: Number,
  lng: Number,
  staff: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Staff' }],
  jobDone: { type: Boolean, default: false}
});

module.exports = mongoose.model('Appointment', appointmentSchema);

和工作人员

var mongoose = require('mongoose');

//Creates the appointments Schema for the Staff details

var staffSchema = new mongoose.Schema({
  name: String, 
  email: { type: String, unique: true },
  phone: Number,
  signup_date: Date,
  jobs: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Appointments' }],
  total_paid: { type: Number, default: 0 }
});

module.exports = mongoose.model('Staff', staffSchema);

0 个答案:

没有答案