I have been following the tutorial on MEAN stack ( https://thinkster.io/mean-stack-tutorial/ ) and have stumbled on a problem. When trying to do npm start
, I am getting an error message, specifically the following:
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
The following are the files that likely contain the problem. I tried tinkering around different lines, mainly in routes\\index.ejs
, but I have not been able to fix this problem yet.
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);
});
});
...
models\\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);
};
public\\javascripts\\angularApp.js
var mongoose = require('mongoose');
require('./models/Posts');
require('./models/Comments');
mongoose.connect('mongodb://localhost/news');
var app = angular.module('linked', ['ui.router']);
Please let me know if I anything is unclear.
The issue is that you are not including the models files in your index.ejs files - you are just referencing model references within mongoose.
Where you have
var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
Replace these with require statements that link to your models/Posts.js file
var Post = require('models/Posts');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.