Created
June 14, 2017 23:55
-
-
Save serhalp/cfd9e1af047a0b57034d0842d8a25e2a to your computer and use it in GitHub Desktop.
Repro for mongoose Model.populate throwing uncaught synchronous MissingSchemaError.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
module.exports = mongoose.model('Bar', new mongoose.Schema({ | |
baz: String, | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose'); | |
module.exports = mongoose.model('Foo', new mongoose.Schema({ | |
bar: { | |
type: mongoose.Schema.ObjectId, | |
ref: 'Bar', | |
}, | |
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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