我正在学习平均堆栈,当我try 使用
npm start
我得到一个例外,说:
schema hasn't been registered for model 'Post'. Use mongoose.model(name, schema)
这是我的代码/模型/帖子.js
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
mongoose.model('Post', PostSchema);
正如我所看到的,应该为模型"Post"注册模式,但是什么可能导致抛出异常呢?
提前谢谢.
编辑:以下是异常错误
/home/arash/Documents/projects/personal/flapper-news/node_modules/mongoose/lib/index.js:323
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Post".
Use mongoose.model(name, schema)
这是应用程序.带有mongoose初始化的js代码:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/news');
require('./models/Posts');
require('./models/Comments');
排队前:
app.use('/', routes);