尚未为模型"Post"注册AngularJS Schema [英] AngularJS Schema hasn't been registered for model 'Post'

查看:86
本文介绍了尚未为模型"Post"注册AngularJS Schema的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习一些MEAN堆栈,然后开始在 thinkster .io ,我说到了需要在服务器端使用的部分,说实话,在某些概念上,我遇到了很多困难,有一些我正在追赶,但大多数我仍然有点迷路了.所以我请求你们的帮助.

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.

第一个文件 App.js :

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;

第二个文件: Index.js

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

第三个文件,我在其中创建了Post和Comment模型:

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:

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

当我执行npm开始运行Web时,它给我显示未为模型"Post"注册Schema的错误. 你能帮助我吗? 预先谢谢你!

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!

推荐答案

在app.js文件中查看require的顺序,请确保具有

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

require('./models/Posts');

在行之前

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

在需要它们的地方.

这篇关于尚未为模型"Post"注册AngularJS Schema的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆