Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: "The 2nd parameter to mongoose.model() should be a schema or a POJO" after upgrading mongoose (discriminator related) #4738

Closed
antonioaltamura opened this issue Nov 24, 2016 · 10 comments

Comments

@antonioaltamura
Copy link

antonioaltamura commented Nov 24, 2016

I'm not sure what's the problem, after upgrading mongoose I get the error in title at the row
var doc = mongoose.model('doc',docSchema);

The entire code

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var caseNote = mongoose.model('caseNote');
var ObjectId=mongoose.Schema.Types.ObjectId;
var models = require('./');
var autopopulate = require('mongoose-autopopulate');

var docSchema_raw = new Schema({
    decidingBody:{type:String},
},{discriminatorKey : '_type'});

var docSchema = caseNote.discriminator('doc',  docSchema_raw);

var doc = mongoose.model('doc',docSchema);

exports.docSchema = docSchema;
module.exports = doc;
@antonioaltamura antonioaltamura changed the title Error: The 2nd parameter to mongoose.model() should be a schema or a POJO after upgrading mongoose Error: "The 2nd parameter to mongoose.model() should be a schema or a POJO" after upgrading mongoose (discriminator related) Nov 24, 2016
@adeelhussain
Copy link

@antonioaltamura why you are registering your doc model again?
try this.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var caseNote = mongoose.model('caseNote');
var ObjectId=mongoose.Schema.Types.ObjectId;
var models = require('./');
var autopopulate = require('mongoose-autopopulate');

var docSchema_raw = new Schema({
    decidingBody:{type:String},
},{discriminatorKey : '_type'});

var docSchema = caseNote.discriminator('doc',  docSchema_raw);

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

exports.docSchema = docSchema;
module.exports = doc;

@antonioaltamura
Copy link
Author

antonioaltamura commented Nov 25, 2016

I'm sorry, I don't get what exactly means
var doc = mongoose.model('doc');

which schema am I defining as 'linked' to the model doc?

@adeelhussain
Copy link

Did you try to run my block of code?

@vkarpov15
Copy link
Collaborator

Why are you passing a model as the second parameter to mongoose.model()? That's not right. I'd imagine you just want to export docSchema, which is misleadingly named because it isn't a schema, it's actually a model.

@antonioaltamura
Copy link
Author

antonioaltamura commented Nov 28, 2016

So the right way to register doc as inherited model of the model caseNote is simply
var docSchema = caseNote.discriminator('doc', docSchema_raw); var doc = mongoose.model('doc');

just to be sure :)

@adeelhussain
Copy link

Exactly :)

@vkarpov15
Copy link
Collaborator

I think you're conflating documents and models. mongoose.model() returns a model, and then new MyModel() gives you a document.

@antonioaltamura
Copy link
Author

antonioaltamura commented Nov 29, 2016

There is no new Model here :)
I'm in doubt how the doc model is defined by the docSchema using @adeelhussain (working) code :)
I guess I should rewatch discriminator docs.

@Partho-FRz
Copy link

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var caseNote = mongoose.model('caseNote');
var ObjectId=mongoose.Schema.Types.ObjectId;
var models = require('./');
var autopopulate = require('mongoose-autopopulate');

var docSchema = new mongoose.Schema({
decidingBody:{type:String},
},{discriminatorKey : '_type'});

var doc = mongoose.model('doc',docSchema);

exports.docSchema = docSchema;
module.exports = doc;

//this should for You :)

@bvedad
Copy link

bvedad commented Mar 19, 2019

Error occurs because discriminator method returns model object but mongoose.model method expects schema. This should be solved with:
var docSchema = caseNote.discriminator('doc', docSchema_raw);
var doc = mongoose.model('doc',docSchema.scheme);

@Automattic Automattic locked as resolved and limited conversation to collaborators Mar 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants