我正在使用 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();
};