0

我正在使用 ActionHero js 和 Mongoose 创建一些休息 API。我将 Mongoose 代码放在初始化程序中,一切正常。当我修改一些文件时,项目会自动重新编译并返回以下错误:OverwriteModelError:

User编译后无法覆盖模型。

我应该如何编辑我的代码以避免此错误?'使用严格';

var mongoose   = require('mongoose');


exports.mongo = function(api, next) {

    mongoose.connect(api.config.mongo.host);

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
        console.log('Connection opened');
    });

    var Schema = mongoose.Schema,
        Types = mongoose.Schema.Types;

    var userSchema = mongoose.Schema({
        createdAt: { type: Date, default: Date.now(), required: true},
        updatedAt: { type: Date, required: false},
        email: { type: String, required: true },
        name: { type: String, required: true },
        surname: { type: String, required: true },
        password: { type: String, required: true },
        roles: [],
        tokens: [{
            code: String,
            expiryDate: { type: Date, default: Date.now() + 30 }
        }]
    });


    var User = mongoose.model('User', userSchema);

    var postSchema = mongoose.Schema({
        createdAt: { type: Date, default: Date.now(), required: true},
        updatedAt: { type: Date, required: false},
        content: { type: String, required: true },
        votes: { type: [Types.ObjectId], ref: 'User' } ,
        coordinates: { type: [Number], index: { type: '2dsphere' }, required: true },
        creator: { type: Schema.Types.ObjectId, ref: 'User', required: true }
    });


    var Post = mongoose.model('Post', postSchema);

    api.mongo = {
        mongoose: mongoose,
        user: User,
        post: Post
    };

    next();
};
4

1 回答 1

1

如果您处于开发模式,actionhero 将重新加载任何初始化程序。您应该将连接步骤包装在 _start() 块中,而不是让它们每次都内联运行。这样,actionhero 可以重新加载文件,而不是重新运行您的连接步骤。

http://actionherojs.com/docs/core/initializers.html

于 2014-06-30T08:11:40.770 回答