Linking 2 Mongoose Patterns

I have two circuits, a Teamand a Match. I want to use Team Schemato define commands in Match Schema. So far these are my Team and Match JS files. I want to associate a command scheme with my correspondence scheme so that I can simply identify the home or remote command, and therefore I save the actual command object in the correspondence scheme.

Thus, I can refer to the home command, for example, to Match.Teams.home.name = England(this, of course, is an example)

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);

And here is what I'm trying to do with 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);
+5
source share
2 answers

Your solution: use the actual schema, not a model using the schema:

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

For

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

var definition = require('path/to/js');, definition.schema

+2

All Articles