gpt4 book ai didi

javascript - MissingSchemaError : Schema hasn't been registered for model

转载 作者:IT老高 更新时间:2023-10-28 13:04:17 27 4
gpt4 key购买 nike

我有一个典型的 Node.js 项目 - Express 3 - MongoDB

我正在尝试在我的/routes/index.js 中对我的模型“Tweet”进行查询,但当我运行我的应用程序时崩溃了

24 Aug 11:35:07 - [nodemon] starting `node app.js`

/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Teewt".
Use mongoose.model(name, schema)
at Mongoose.model (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286:13)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/routes/index.js:6:33)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/app.js:7:14)
at Module._compile (module.js:456:26)
24 Aug 11:35:07 - [nodemon] app crashed - waiting for file changes before starting...

这是我的 app.js 的一部分

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/fanOcesa');
var Schema = mongoose.Schema;
var ObjectID = Schema.ObjectID;

var Teewt = new Schema({
cuerpo: String
});

var Teewt = mongoose.model('Teewt', Teewt);

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);

这是我的 index.js 的一部分

var Teewt = require('mongoose').model('Teewt');

Teewt.find({}, function(err, docs){
console.log('docs');
console.log(docs);
});

exports.index = function(req, res){
res.render('index', {
docs: docs
});
};

执行此查询的正确方法是什么?

最佳答案

index.js 文件在 app.js 文件调用的地方执行:

var routes = require('./routes');

因此,请确保在 您调用将 'Teewt' 模式注册为 app.js 中的 Mongoose 模型之后调用它。

关于javascript - MissingSchemaError : Schema hasn't been registered for model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18420920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com