简体   繁体   中英

Cucumber + Mongoose is giving me Schema hasn't been registered for model

The error message:

MissingSchemaError: Schema hasn't been registered for model "User".
Use mongoose.model(name, schema)
at Mongoose.model  (/Users/JimBarrows/Desktop/TaskVelocity/cucubmer/node_modules/mongoose/lib/index.js:349:13)
at Object.<anonymous> (/Users/JimBarrows/Desktop/TaskVelocity/cucubmer/features/support/hooks.js:3:21)

My world.js file:

/**stuff**/
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
require('../../../public_ui/models/Users');
var User = mongoose.model('User');
mongoose.connect('mongodb://192.168.99.100:27017/task_velocity');

As I understand it, this should setup mongoose quite nicely. Then in hooks.js:

'use strict';
 var mongoose = require('mongoose');
 var User = mongoose.model('User');

I've tried, in the hooks.js file:

'use strict';
var mongoose = require('mongoose');
require('../../../public_ui/models/Users');
var User = mongoose.model('User');

but that gives me the same error when I try to get ahold of the model.

Any ideas on what I'm doing wrong?

Jim Barrows, considering that the error is:

MissingSchemaError: Schema hasn't been registered for model " User ".

and that your world.js has the following lines:

require('../../../public_ui/models/ User s');

var User = mongoose.model(' User ');

I believe that is just a typo error and you could fix it easily by doing the following:

var User = mongoose.model('Users'); // instead of var User = mongoose.model('User');

**which by the way is exactly what dafyk commented in your post (not an answer) some days ago...

Hope this helps

I'm not sure, but...

check this link

You have to make schema and set mongoose.model('User',schema);

Can you show me "Users.js" source?; If you want make module, like this source

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

    var UserSchema = new Schema({
        username: String,
        passwrod: String
        //Have to Custom
    });

    module.exports = mongoose.model('User', UserSchema);

and use this

var Users = require('../../../public_ui/models/Users');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM