schema hasnt been registred for model (MEAN STACK)

◇◆丶佛笑我妖孽 提交于 2019-12-08 10:34:13

问题


I'm learning the mean stack and when I try to start the server using

npm start

I get an exception saying that:

      throw new mongoose.Error.MissingSchemaError(name);
      ^
MissingSchemaError: Schema hasn't been registered for model "Firms".

here is my code Firms.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' }
});

CommentSchema.methods.upvote = function(cb) {
  this.upvotes += 1;
  this.save(cb);
};

CommentSchema.methods.downvote = function(cb) {
  this.upvotes -= 1;
  this.save(cb);
};

mongoose.model('Firms', CommentSchema);

as I can see the schema should be registered for the model 'Firms', but what can be possibly causing the exception to be thrown?

here's the app.js code with the mongoose initialization:

   var mongoose = require('mongoose');
var express = require('express');
var router = express.Router();
var passport = require('passport');
var jwt = require('express-jwt');

var Post = mongoose.model('Post');
var Comment = mongoose.model('Comment');
var Firm = mongoose.model('Firms');
var User = mongoose.model('User');

//var Crime = mongoose.model('Crime');



var auth = jwt({secret: 'SECRET', userProperty: 'payload'});


/* GET home page. */
router.get('/', function(req, res) {
  res.render('index');
});

回答1:


Try using module.exports = mongoose.model('Firms', CommentSchema); also in your app.js try var Firms = require('Firms');



来源:https://stackoverflow.com/questions/38957777/schema-hasnt-been-registred-for-model-mean-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!