[英]ActionHero js with Mongoose
I am creating some rest API with ActionHero js and Mongoose. 我正在使用ActionHero js和Mongoose创建一些其他API。 I put the Mongoose code in an initalizers and everything works. 我将Mongoose代码放在了一个初始化器中,一切正常。 When I modify some files the project automatically recompiles and it returns the following error: OverwriteModelError: 当我修改某些文件时,项目将自动重新编译,并返回以下错误:OverwriteModelError:
Cannot overwrite
User
model once compiled. 编译后无法覆盖User
模型。
How should I edit my code to avoid this error? 我应该如何编辑代码以避免此错误? 'use strict'; “使用严格”;
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();
};
actionhero will reload any initializers if you are in developmentMode. 如果您处于developmentMode,则actionhero将重新加载所有初始化程序。 You should wrap your connection steps within the _start() block, rather than have them run in-line each time. 您应该将连接步骤包装在_start()块中,而不是每次都在它们中串联运行。 This way, actionhero can re-load the file and not re-run your connection steps. 这样,actionhero可以重新加载文件,而无需重新运行连接步骤。
http://actionherojs.com/docs/core/initializers.html http://actionherojs.com/docs/core/initializers.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.