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

Missing Schema Error when Populating null references #9340

Closed
dataclear opened this issue Aug 17, 2020 · 1 comment
Closed

Missing Schema Error when Populating null references #9340

dataclear opened this issue Aug 17, 2020 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@dataclear
Copy link

Do you want to request a feature or report a bug?
bug

What is the current behavior?
When populating a model with a refPath that hasn't been registered, mongoose gives a schema error even if the reference ID contains no values. This wasn't giving an error up until mongoose@5.8.4

If the current behavior is a bug, please provide the steps to reproduce.

The script below works up to mongoose 5.8.3 and fails for any versions beyond.

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

mongoose.connect('mongodb://localhost:27017/schemaerror');
var db = mongoose.connection;
db.on('error', console.error.bind(console,'MongoDB connection error:'));

const firstSchema = new Schema({
  ref: String,
  linkedId: {
    type: Schema.ObjectId,
    refPath: 'ref'
  }
});

const firstModel = mongoose.model('first', firstSchema);

const secondSchema = new Schema({
  foo: String
});

const secondModel = mongoose.model('second', secondSchema);

const displayError = async () => {

  try {
    let newSecond = await secondModel.create({foo: 'bar'});
    let populatedFirst = await firstModel.create({ref: 'second', linkedId: newSecond._id});
    let unpopulatedFirst = await firstModel.create({ref: 'third'});
    let result = await firstModel.find({}).populate('linkedId').exec();
    console.log('success');
    process.exit;
  } catch (error) {
    console.error(error);
  }

}

displayError();

The following error is produced:

MissingSchemaError: Schema hasn't been registered for model "third".
Use mongoose.model(name, schema)
    at NativeConnection.Connection.model (...\node_modules\mongoose\lib\connection.js:1192:11)
    at getModelsMapForPopulate (...\node_modules\mongoose\lib\helpers\populate\getModelsMapForPopulate.js:283:59)
    at populate (...\node_modules\mongoose\lib\model.js:4309:21)

What is the expected behavior?
No errors when populating empty references.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 12.18.3
Mongoose: 5.10.0
MongoDB: 4.2.7

@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 26, 2020
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 26, 2020
vkarpov15 added a commit that referenced this issue Aug 26, 2020
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 26, 2020
@Uzlopak

This comment was marked as outdated.

@Automattic Automattic deleted a comment from kyle-belle Jun 17, 2022
@Automattic Automattic locked and limited conversation to collaborators Jun 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants