[英]MissingSchemaError in NodeJS app on Heroku server
我的應用程序已成功在我的本地計算機上運行。
當我將其推送到heroku服務器時,有時會因以下錯誤而崩潰:
2012-12-28T10:00:53+00:00 heroku[web.1]: Starting process with command `node server.js`
2012-12-28T10:00:54+00:00 app[web.1]:
2012-12-28T10:00:54+00:00 app[web.1]: /app/node_modules/mongoose/lib/index.js:261
2012-12-28T10:00:54+00:00 app[web.1]: throw new mongoose.Error.MissingSchemaError(name);
2012-12-28T10:00:54+00:00 app[web.1]: ^
2012-12-28T10:00:54+00:00 app[web.1]: MissingSchemaError: Schema hasn't been registered for model "Activity".
2012-12-28T10:00:54+00:00 app[web.1]: Use mongoose.model(name, schema)
2012-12-28T10:00:54+00:00 app[web.1]: at Mongoose.model (/app/node_modules/mongoose/lib/index.js:261:13)
2012-12-28T10:00:54+00:00 app[web.1]: at Object.<anonymous> (/app/app/models/user.js:8:25)
2012-12-28T10:00:54+00:00 app[web.1]: at Module._compile (module.js:449:26)
2012-12-28T10:00:54+00:00 app[web.1]: at Object.Module._extensions..js (module.js:467:10)
2012-12-28T10:00:54+00:00 app[web.1]: at Module.load (module.js:356:32)
2012-12-28T10:00:54+00:00 app[web.1]: at Function.Module._load (module.js:312:12)
2012-12-28T10:00:54+00:00 app[web.1]: at Module.require (module.js:362:17)
2012-12-28T10:00:54+00:00 app[web.1]: at require (module.js:378:17)
2012-12-28T10:00:54+00:00 app[web.1]: at /app/server.js:23:3
2012-12-28T10:00:54+00:00 app[web.1]: at Array.forEach (native)
2012-12-28T10:00:55+00:00 heroku[web.1]: Process exited with status 1
2012-12-28T10:00:55+00:00 heroku[web.1]: State changed from starting to crashed
在我的server.js中,我正在使用以下代碼加載模型:
var models_path = __dirname + '/app/models'
fs.readdirSync(models_path).forEach(function (file) {
require(models_path+'/'+file)
})
我的activity.js是:
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, moment = require('moment')
var schemaOptions = {
toJSON: {
virtuals: true
}
};
var ActivitySchema = new Schema({
venue: {type : Schema.ObjectId, ref : 'Venue'}
, user: {type : Schema.ObjectId, ref : 'User'}
, createdAt: {type : Date, default : Date.now}
, rate: Number
, message : String
, source : String
}, schemaOptions)
mongoose.model('Activity', ActivitySchema)
ActivitySchema.index({ "user": 1, "venue" : 1 }, { unique: true })
var modifiedAt = require('../../config/plugins.js');
ActivitySchema.plugin(modifiedAt, { index: false });
ActivitySchema.virtual('summary').get(function () {
moment.lang('en');
return moment(this.createdAt).fromNow() + ' via ' + this.source;
});
奇怪的是,有時應用程序崩潰了,但有時卻能正常工作。 我該怎么解決?
為了解決貓鼬中的“ MissingSchemaError”,我在代碼中要做的就是導出一個以貓鼬為參數的函數。
這樣的事情。
module.exports = function (mongoose) {
var GeoSchema = new mongoose.Schema({
loc:{ type:[Number], index:'2d'}
});
return GeoSchema;
};
我遇到了同樣的問題。 我已經修改了我的用戶模塊,因此它將創建一個與用戶模塊相對應的相應概要文件。 將那個用戶模塊加載到Profile模式時,將其加載到文件頂部時會拋出相同的錯誤。 當我將其放在代碼范圍內時,我正在使用它來解決此問題。
var Profile = mongoose.model('Profile');
我不得不將上面的代碼移到代碼中(我認為這是一個時機,我試圖在將mongo制作為
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.