Skip to content

Commit c9842d3

Browse files
IslandRhythmsvkarpov15
authored andcommitted
fix: diffIndexes treats namespace error as empty
1 parent bbbfa95 commit c9842d3

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/model.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,12 @@ Model.diffIndexes = async function diffIndexes() {
15161516

15171517
const model = this;
15181518

1519-
let dbIndexes = await model.listIndexes();
1519+
let dbIndexes = await model.listIndexes().catch(err => {
1520+
if (err.codeName == 'NamespaceNotFound') {
1521+
return undefined;
1522+
}
1523+
throw err;
1524+
});
15201525
if (dbIndexes === undefined) {
15211526
dbIndexes = [];
15221527
}

test/model.indexes.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,5 +714,15 @@ describe('model', function() {
714714
assert.deepStrictEqual(result.toDrop, ['age_1', 'weight_1']);
715715
assert.deepStrictEqual(result.toCreate, [{ password: 1 }, { email: 1 }]);
716716
});
717+
718+
it('running diffIndexes with a non-existent collection should not throw an error (gh-14010)', async function() {
719+
const testSchema = new mongoose.Schema({
720+
name: String
721+
});
722+
723+
const Test = db.model('gh14010', testSchema);
724+
const res = await Test.diffIndexes();
725+
assert.ok(res);
726+
});
717727
});
718728
});

0 commit comments

Comments
 (0)