MissingSchemaError:架构尚未注册模型“Post”

我一直在关于MEAN栈( https://thinkster.io/mean-stack-tutorial/ )上的教程,并偶然发现了一个问题。 当试图执行npm start ,我收到一条错误消息,具体如下:

 C:\Linked\linked>npm start > linked@0.0.0 start C:\Linked\linked > node ./bin/www C:\Linked\linked\node_modules\mongoose\lib\index.js:329 throw new mongoose.Error.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model "Post". Use mongoose.model(name, schema) at Mongoose.model (C:\Linked\linked\node_modules\mongoose\lib\index.js:329:13) at Object.<anonymous> (C:\Linked\linked\routes\index.js:2:21) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (C:\Linked\linked\app.js:8:14) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Module.require (module.js:365:17) at require (module.js:384:17) at Object.<anonymous> (C:\Linked\linked\bin\www:7:11) npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "start" npm ERR! node v0.12.3 npm ERR! npm v2.9.1 npm ERR! code ELIFECYCLE npm ERR! linked@0.0.0 start: `node ./bin/www` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the linked@0.0.0 start script 'node ./bin/www'. npm ERR! This is most likely a problem with the linked package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node ./bin/www npm ERR! You can get their info via: npm ERR! npm owner ls linked npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! C:\Linked\linked\npm-debug.log 

以下是可能包含该问题的文件。 我试着在不同的路线上修改,主要是在routes\index.ejs ,但是我还没能解决这个问题。

路线\ index.ejs

 var mongoose = require('mongoose'); var Post = mongoose.model('Post'); var Comment = mongoose.model('Comment'); var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); module.exports = router; // GET posts router.get('/posts', function(req, res, next) { Post.find(function(err, posts){ if(err){ return next(err); } res.json(posts); }); }); // POST a post router.post('/posts', function(req, res, next) { var post = new Post(req.body); post.save(function(err, post){ if(err){ return next(err); } res.json(post); }); }); ... 

型号\ Posts.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); // Upvote method PostSchema.methods.upvote = function(cb) { this.upvotes += 1; this.save(cb); }; 

公共\ JavaScript的\ angularApp.js

 var mongoose = require('mongoose'); require('./models/Posts'); require('./models/Comments'); mongoose.connect('mongodb://localhost/news'); var app = angular.module('linked', ['ui.router']); 

请让我知道,如果我什么都不清楚。

问题是你没有在你的index.ejs文件中包含模型文件 – 你只是在mongoose内引用模型引用。

你在哪里

 var Post = mongoose.model('Post'); var Comment = mongoose.model('Comment'); 

将这些replace为链接到您的模型/ Posts.js文件的require语句

 var Post = require('models/Posts');