尚未为模型注册架构(MEAN STACK) [英] schema hasnt been registred for model (MEAN STACK)
本文介绍了尚未为模型注册架构(MEAN STACK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习平均堆栈,当我尝试使用
I'm learning the mean stack and when I try to start the server using
npm start
我得到一个例外:
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Firms".
这是我的代码Firms.js
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);
如我所见,应该为模型"Firms"注册模式,但是什么可能导致引发异常?
as I can see the schema should be registered for the model 'Firms', but what can be possibly causing the exception to be thrown?
这是带有猫鼬初始化的app.js代码:
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');
});
推荐答案
在app.js中也尝试使用module.exports = mongoose.model('Firms', CommentSchema);
尝试var Firms = require('Firms');
Try using module.exports = mongoose.model('Firms', CommentSchema);
also in your app.js try var Firms = require('Firms');
这篇关于尚未为模型注册架构(MEAN STACK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文