问题
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