Skip to content

Instantly share code, notes, and snippets.

@serhalp
Created June 14, 2017 23:55
Show Gist options
  • Save serhalp/cfd9e1af047a0b57034d0842d8a25e2a to your computer and use it in GitHub Desktop.
Save serhalp/cfd9e1af047a0b57034d0842d8a25e2a to your computer and use it in GitHub Desktop.
Repro for mongoose Model.populate throwing uncaught synchronous MissingSchemaError.
const mongoose = require('mongoose');
module.exports = mongoose.model('Bar', new mongoose.Schema({
baz: String,
}));
const mongoose = require('mongoose');
module.exports = mongoose.model('Foo', new mongoose.Schema({
bar: {
type: mongoose.Schema.ObjectId,
ref: 'Bar',
},
}));
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const Foo = require('./foo');
// Uncomment to "fix":
// const Bar = require('./foo');
process.on('uncaughtException', function (err) {
console.log('Uncaught exception!', err);
process.exit(1);
});
mongoose.connect('mongodb://localhost/test_mongoose_populate_uncaught')
.then(repro)
.then(() => console.log('resolved'))
.catch((err) => console.log('rejected with', err))
.then(() => mongoose.disconnect());
function repro () {
try {
return Foo.find().populate('bar').exec();
} catch (err) {
console.log('I can catch it!');
}
}
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const Foo = require('./foo');
const Bar = require('./bar');
mongoose.connect('mongodb://localhost/test_mongoose_populate_uncaught')
.then(setup)
.then(() => mongoose.disconnect());
function setup () {
return Bar.create({baz: 'abc'})
.then((bar) => Foo.create({bar}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment