简体   繁体   English

Heroku服务器上的NodeJS应用中的MissingSchemaError

[英]MissingSchemaError in NodeJS app on Heroku server

My app is successfully works on my local. 我的应用程序已成功在我的本地计算机上运行。

When I push it to a heroku server, sometimes it crashes with this error: 当我将其推送到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

In my server.js, I am loading models with this code: 在我的server.js中,我正在使用以下代码加载模型:

var models_path = __dirname + '/app/models'
fs.readdirSync(models_path).forEach(function (file) {
     require(models_path+'/'+file)
})

And my activity.js is: 我的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;
});

Weird thing is, sometimes the app crashes but sometimes it works. 奇怪的是,有时应用程序崩溃了,但有时却能正常工作。 What can I do to solve it? 我该怎么解决?

What I had to do in my code to resolve the 'MissingSchemaError' in mongoose was to export a function that takes mongoose as the parameter. 为了解决猫鼬中的“ MissingSchemaError”,我在代码中要做的就是导出一个以猫鼬为参数的函数。

Something like this. 这样的事情。

module.exports = function (mongoose) {

    var GeoSchema = new mongoose.Schema({
        loc:{ type:[Number], index:'2d'}
    });

    return GeoSchema;
};

I ran into the same issue. 我遇到了同样的问题。 I had modified my users module so it would create a corresponding profile document that corresponded with the users module. 我已经修改了我的用户模块,因此它将创建一个与用户模块相对应的相应概要文件。 Having that users module load the Profile schema would throw the same error when I loaded it at the top of the file. 将那个用户模块加载到Profile模式时,将其加载到文件顶部时会抛出相同的错误。 When I put it within the scope of the code I was using it fixed the problem. 当我将其放在代码范围内时,我正在使用它来解决此问题。

    var Profile = mongoose.model('Profile');

I had to move the above line into the code (I think it's a timing thing, where I was trying to use it before mongo had been made a 我不得不将上面的代码移到代码中(我认为这是一个时机,我试图在将mongo制作为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM