首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Heroku服务器上NodeJS应用程序中的MissingSchemaError

Heroku服务器上NodeJS应用程序中的MissingSchemaError
EN

Stack Overflow用户
提问于 2012-12-28 18:09:41
回答 2查看 878关注 0票数 1

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

当我将其推送到heroku服务器时,有时它会崩溃,并显示以下错误:

代码语言:javascript
复制
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中,我使用以下代码加载模型:

代码语言:javascript
复制
var models_path = __dirname + '/app/models'
fs.readdirSync(models_path).forEach(function (file) {
     require(models_path+'/'+file)
})

我的activity.js是:

代码语言:javascript
复制
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;
});

奇怪的是,有时应用程序会崩溃,但有时它还能工作。我能做些什么来解决这个问题?

EN

回答 2

Stack Overflow用户

发布于 2013-01-04 03:23:10

我必须在我的代码中解析mongoose中的“MissingSchemaError”,就是导出一个以mongoose为参数的函数。

就像这样。

代码语言:javascript
复制
module.exports = function (mongoose) {

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

    return GeoSchema;
};
票数 0
EN

Stack Overflow用户

发布于 2015-02-22 08:40:54

我遇到了同样的问题。我修改了我的users模块,这样它就会创建一个与users模块相对应的配置文件文档。当我在文件顶部加载配置文件模式时,让该用户模块加载配置文件模式也会抛出相同的错误。当我把它放在我正在使用的代码范围内时,就解决了这个问题。

代码语言:javascript
复制
    var Profile = mongoose.model('Profile');

我不得不将上面的代码行移动到代码中(我认为这是一个计时的问题,在mongo成为

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14067890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档