我一直在关注MEAN堆栈(https://jsfid)的教程,并且偶然发现了一个问题。尝试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
以下是可能包含此问题的文件。我尝试修改不同的行,主要是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);
});
});
...
,但我还没有解决这个问题。
路由\ index.ejs
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);
};
模型\ Posts.js
var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');
mongoose.connect('mongodb://localhost/news');
var app = angular.module('linked', ['ui.router']);
公开\ Javascript角\ angularApp.js
rm -frd ~/Library/Developer/Xcode/DerivedData/*
如果我不清楚,请告诉我。
答案 0 :(得分:1)
问题是你没有在你的index.ejs文件中包含模型文件 - 你只是在mongoose中引用模型引用。
你在哪里
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
将这些替换为链接到models / Posts.js文件的require语句
var Post = require('models/Posts');