如何在angularjs中反映mongoose中的两个模式

时间:2016-02-18 09:29:27

标签: javascript angularjs mongodb mongoose

解决我的忽视......我无法理解猫鼬如何用参考作用" 人口"也许......例如,如果学校是一个架构而学生是其他...并且如果学生对学校架构如何...? 学校架构:

'use strict';

var mongoose = require('mongoose');
var schoolSchema = new mongoose.Schema({
    organizationName:   { type: String },
    Location:   { type: String },
    students: [{ type: Schema.Types.ObjectId, ref: 'students' }]
}); 
module.exports = mongoose.model('school', schoolSchema);

console.log("school schema defined")


学生架构:

'use strict';

var mongoose = require('mongoose');
var studentsSchema = new mongoose.Schema({
    name:   { type: String },
    class:  { type: String },

}); 
module.exports = mongoose.model('students', studentsSchema);

console.log("students schema defined")

如果我有单独的服务


学校中间件:

'use strict';

var express = require('express');
var router = express.Router();
var schoolModel = require('../model/schoolSchema');

 router.get('/', function(req, res, next) {
      schoolModel.find(function (err, data) {
      if (err) return next(err);
      res.json(data);
    });
  },
 router.post('/', function(req, res, next) {
     schoolModel.create(req.body, function (err, post) {
      if (err) return next(err);
      res.json(post);
    });
  }
module.exports = router

类似学生中间件

并且在角度控制器中我不知道如何使用refference obj调用!!!但我做了一些事情

'use strict';

    var app =angular.module('school');
        app.controller('controller', ['$scope', '$cookieStore','$http', controller]);

    function controller($scope, $cookieStore, $http) {
//getting all data
    var refresh = function() {
        $http.get('/schoolApi').success(function(response) {
          $scope.dataSchool = response;
          $scope.school = "";
          });
      };
    refresh();
//add data
    $scope.add = function(){
        var inData       = $scope.school;
        inData.students = $scope.students;
        console.log(inData);
        $http.post('/schoolApi',inData).success(function(response) {
            console.log(response);
            refresh();
        });
    };


提前致谢

0 个答案:

没有答案