我有两个模式,TeamMatch。我想使用Team Schema来标识Match Schema中的团队。到目前为止,这是我的Team and Match JS文件。我想将团队架构链接到我的比赛架构,以便我可以简单地识别主队或客队,以便在比赛架构中存储一个实际的团队对象。

这样,我可以将主队称为Match.Teams.home.name = England(当然,这只是一个示例)

Team.js

'use strict';

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var validatePresenceOf = function(value){
  return value && value.length;
};

var getId = function(){
  return new Date().getTime();
};

/**
  * The Team schema. we will use timestamp as the unique key for each team
  */
var Team = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'name' : { type : String,
              validate : [validatePresenceOf, 'Team name is required'],
              index : { unique : true }
            }
});

module.exports = mongoose.model('Team', Team);

这就是我想用Match.js做的事情
'use strict';

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TeamSchema = require('mongoose').model('Team');

var validatePresenceOf = function(value){
  return value && value.length;
};

var toLower = function(string){
  return string.toLowerCase();
};

var getId = function(){
  return new Date().getTime();
};

/**
  * The Match schema. Use timestamp as the unique key for each Match
  */
var Match = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'hometeam' : TeamSchema,
  'awayteam' : TeamSchema
});

module.exports = mongoose.model('Match', Match);

最佳答案

您的解决方案:使用实际模式,而不是使用模式的模型:

module.exports = mongoose.model('Team', Team);


module.exports = {
    model: mongoose.model('Team', Team),
    schema: Team
};

然后使用var definition = require('path/to/js');,并直接使用definition.schema而不是模型

07-25 22:01
查看更多