问题
When i called my function in controller through postman,it worked fine and i got results but when i wrote a test case for the same function it is saying the error as
MissingSchemaError: Schema hasn't been registered for model "categories".
Use mongoose.model(name, schema)
I was completely confused about these because how come a function that is working well go wrong with test case.Can any one suggest help please. My test.js,
var server = require('../modules/categories/model/categories.server.model');
var chai = require('chai');
var chaiHttp = require('chai-http');
var should = chai.should();
var mongoose = require('mongoose');
chai.use(chaiHttp);
describe('Blobs', function() {
it('should list ALL blobs on /getcategories GET');
});
it('should list ALL categories on /getcategories', function(done) {
chai.request(server)
.get('getcategories')
.end(function(err, res){
res.should.have.status(200);
done();
});
});
My Routes,
*/
var categories = require('../categories/controller/categories.server.controller');
var model = require('../categories/model/categories.server.model');
var passport = require('passport');
module.exports = function (app) {
// User Routes
app.use(function (req, res, next) {
// Setting up the users password api
app.route('/api/auth/insertcategory').post(categories.insertcategory);
app.route('/api/auth/getcategoriesbycreatedid/:id').get(categories.getcategoriesbycreatedid);
app.route('/api/auth/getcategories').get(categories.getcategories);
});
Can some one please suggest help.
回答1:
This issue normally raise when you try to use your
modelin anywhere before interpreting it.
Try to add your models before all the controllers and routes
Like this
var model = require('../categories/model/categories.server.model');
var categories = require('../categories/controller/categories.server.controller');
var passport = require('passport');
Hope this will help you.
来源:https://stackoverflow.com/questions/40231463/schema-hasnt-been-registered-for-model-categories-use-mongoose-modelname-s