61939

AngularJS Schema hasn't been registered for model 'Post'

I am trying to learn a little bit of MEAN stack and I started doing the tutorial on thinkster.io, and I made it to the part where you need to work with the server-side, which honestly I have a lot of difficulties regarding some concepts yet, a few I am catching in, but the majority I still kinda lost. So I ask for your guys help.

First File <strong>App.js</strong>:

var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); var users = require('./routes/users'); var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/news'); require('./models/Posts'); require('./models/Comments'); var app = express(); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); // uncomment after placing your favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handlers // development error handler // will print stacktrace if (app.get('env') === 'development') { app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: err }); }); } // production error handler // no stacktraces leaked to user app.use(function(err, req, res, next) { res.status(err.status || 500); res.render('error', { message: err.message, error: {} }); }); module.exports = app;

Second File: <strong>Index.js</strong>

var express = require('express'); var router = express.Router(); /* GET home page. */ router.get('/', function(req, res) { res.render('index', { title: 'Express' }); }); module.exports = router; var mongoose = require('mongoose'); var Post = mongoose.model('Post'); var Comment = mongoose.model('Comment'); router.get('/posts', function(req, res, next) { Post.find(function(err, posts){ if(err){ return next(err); } res.json(posts); }); }); 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); }); });

Third file, where i created the models for Post and Comment:

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);

Comments.js:

var mongoose = require('mongoose'); var CommentSchema = new mongoose.Schema({ body: String, author: String, upvotes: {type: Number, default: 0}, post: { type: mongoose.Schema.Types.ObjectId, ref: 'Post' } }); mongoose.model('Comment', CommentSchema);

When I do the npm start to run the web it gives me the error of Schema hasn't been registered for model "Post". Can you help me? Thank you in advance!

Answer1:

Check out the order of requires in app.js file, be sure to have

require('./models/Posts');

BEFORE the line

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

where you need them.

Recommend

  • Does pImpl fundamentally solve C++ DLL issue?
  • How to activate JS data-filter when page loads?
  • why route of backbone needs the default route
  • MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
  • How should I start to implement RESTful web service?
  • Manage different base layouts in Angular2
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • PWA with multiple pages
  • Install phpMongo Drivers on windows
  • pymongo replication secondary readreference not work
  • CORS with socket.io
  • What is the use of a session store?
  • mave 3.2 not able to access local nexus instance return 502 code
  • Django model for a Postgres view
  • Tomcat memory Leak
  • Combining two different ActiveRecord collections into one
  • Webgrid not refreshing after delete MVC
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Do I need to reset a Perl hash index?
  • Use of this Javascript
  • Play WS (2.2.1): post/put large request
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Cannot resolve symbol 'MyApi'
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • req.body is undefined - nodejs
  • Symfony2: How to get request parameter
  • Akka Routing: Reply's send to router ends up as dead letters
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • KeystoneJS: Relationships in Admin UI not updating
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • How does Linux kernel interrupt the application?
  • How to Embed XSL into XML
  • git trying to push non-existent file … after clearing cache
  • How to load view controller without button in storyboard?