Mongoose和ExpressJS - 模块化架构?

时间:2016-03-29 10:48:47

标签: mongodb mongoose

我想为每个模型提供每个文件 - 即用户和国家/地区。 目前,我通过以下方式解决了这个问题:

person.js

/*jslint node: true */
'use strict';

var mongoose = require('./mongoose'),
    Schema = mongoose.Schema;

var userSchema = new Schema({
    name: String,
    username: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    admin: Boolean,
    location: String,
    meta: {
        age: Number,
        website: String
    },
    created_at: Date,
    updated_at: Date,
    country: {
        type: Schema.Types.ObjectId,
        ref: 'Country'
    }
});

userSchema.methods.toString = function() {
    return `I am ${this.username} and I'm from ${this.country.name}`;
}

module.exports = mongoose.model('Person', userSchema);

country.js

/*jslint node: true */
'use strict';

var mongoose = require('./mongoose'),   
Schema = mongoose.Schema;

var countrySchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    created_at: Date,
    updated_at: Date,
    people: [{
        type: Schema.Types.ObjectId,
        ref: 'Person'
    }]
});

var Country = mongoose.model('Country', countrySchema);

module.exports = Country;

所以我的问题如下:mongoose如何引用两个表?例如,在Person架构中,我通过提供ref: 'Country'来引用该国家/地区,但我从未将该模型包含在Person.js文件中。我在这里做错了吗?

更新

考虑下面的答案,我现在有3个文件:

database.js

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/people');
mongoose.set('debug', true);

module.exports = mongoose;

person.js

    /*jslint node: true */
'use strict';

var mongoose = require('./database'),
    Schema = mongoose.Schema;

var userSchema = new Schema({
    name: String,
    username: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    admin: Boolean,
    location: String,
    meta: {
        age: Number,
        website: String
    },
    created_at: Date,
    updated_at: Date,
    country: {
        type: Schema.Types.ObjectId,
        ref: 'Country'
    }
});

userSchema.methods.toString = function() {
    return `I am ${this.username} and I'm from ${this.country.name}`;
}

mongoose.model('Person', userSchema);

country.js

    /*jslint node: true */
'use strict';

var mongoose = require('./mongoose'),   
Schema = mongoose.Schema;

var countrySchema = new Schema({
    name: {
        type: String,
        required: true,
        unique: true
    },
    created_at: Date,
    updated_at: Date,
    people: [{
        type: Schema.Types.ObjectId,
        ref: 'Person'
    }]
});

mongoose.model('Country', countrySchema);

现在,当我将所有这3个文件都包含在测试文件中时,例如:

var mongoose = require('./database'),
User = mongoose.model('Person'),
Country = mongoose.model('Country');

我收到以下错误:MissingSchemaError: Schema hasn't been registered for model "Person"。如果删除Country变量,我会User收到同样的错误。似乎mongoose正在尝试定义模型,而不是检索模型。什么似乎是问题?

更新2:

好的,当我现在包含这些文件时,我会执行以下操作:

require('./person');
require('./country');

var User = require('mongoose').model('Person'),
Country = require('mongoose').model('Country');

0 个答案:

没有答案