【问题标题】:MissingSchemaError in NodeJS app on Heroku serverHeroku 服务器上的 NodeJS 应用程序中的 MissingSchemaError
【发布时间】:2012-12-13 14:57:27
【问题描述】:

我的应用在我的本地成功运行。

当我将它推送到 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;
});

奇怪的是,有时应用程序会崩溃,但有时却可以正常工作。我能做些什么来解决它?

【问题讨论】:

    标签: node.js heroku mongoose


    【解决方案1】:

    为了解决 mongoose 中的“MissingSchemaError”问题,我必须在代码中导出一个以 mongoose 作为参数的函数。

    类似的东西。

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。我已经修改了我的用户模块,因此它会创建一个与用户模块相对应的相应配置文件。当我将用户模块加载到文件顶部时,让该用户模块加载配置文件架构会引发相同的错误。当我将它放在我正在使用的代码范围内时,它解决了问题。

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

      我不得不将上面的行移到代码中(我认为这是一个时间问题,我试图在 mongo 被制作之前使用它

      【讨论】:

        猜你喜欢
        • 2017-06-12
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 2019-02-21
        • 2018-04-18
        相关资源
        最近更新 更多