Skip to content

Commit 006f11b

Browse files
committed
init() is not being called when using db.model for some reason
1 parent 05281e4 commit 006f11b

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lib/model.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,7 @@ Model.init = function init(callback) {
12921292
});
12931293
} else {
12941294
_syncIndexes(error => {
1295+
console.log('_syncIndexes')
12951296
if (error) {
12961297
return reject(error);
12971298
}
@@ -1408,6 +1409,7 @@ Model.createCollection = function createCollection(options, callback) {
14081409
*/
14091410

14101411
Model.syncIndexes = function syncIndexes(options, callback) {
1412+
console.log('sync Indexes');
14111413
_checkContext(this, 'syncIndexes');
14121414

14131415
callback = this.$handleCallbackError(callback);

test/index.test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -874,21 +874,20 @@ describe('mongoose module:', function() {
874874
const schema = new m.Schema({
875875
title: {type: String, index: true}
876876
});
877-
const syncFalse = db.model('NoSync', schema, 'NoSync');
877+
const syncFalse = db.model('Sync', schema, 'Sync');
878+
await syncFalse.init();
878879
const Indexes = await syncFalse.listIndexes();
879880
console.log(Indexes);
880881
assert.equal(Indexes.length, 2);
881-
syncFalse.collection.drop();
882-
mongoose.connection.models = {};
882+
await syncFalse.collection.createIndex({name: 1});
883+
delete m.connection.models['Sync'];
883884
m.set('syncIndexes', true);
884-
const syncSchema = new m.Schema({ name: String });
885-
m.connection.db.collection('Sync').createIndex({name: 1});
885+
const syncSchema = new m.Schema({ title: String });
886886
const syncTrue = db.model('Sync', syncSchema, 'Sync');
887+
await syncTrue.init();
887888
const Index = await syncTrue.listIndexes();
888889
console.log(Index);
889-
890-
891-
// https://thecodebarbarian.com/whats-new-in-mongoose-5-2-syncindexes
890+
assert.equal(Index.length, 1);
892891
});
893892
});
894893
});

0 commit comments

Comments
 (0)