Skip to content

Commit 23681de

Browse files
committed
fix model: Cannot read properties of undefined (reading '_id')
1 parent 5e377e8 commit 23681de

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

src/lib/model.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,19 @@ const model = (collectionName = '', schema = {}) => {
241241
};
242242

243243
const deleteItems = async (items) => {
244-
if (items.length > 0) {
244+
if (items && items.length > 0) {
245245
await executeMiddleware('pre', collectionName, 'delete', items);
246246

247247
for (const el of items) {
248-
data[collectionName].delete(el._id);
248+
if (el && el._id) data[collectionName].delete(el._id);
249249
}
250250

251251
storageController.onDelete(collectionName, items);
252252

253253
await executeMiddleware('post', collectionName, 'delete', items);
254-
}
255254

256-
return items;
255+
return items;
256+
}
257257
};
258258

259259
const insertOne = async (item) => {
@@ -317,21 +317,24 @@ const model = (collectionName = '', schema = {}) => {
317317
};
318318

319319
const findByIdAndDelete = async (id) => {
320-
const items = await findById(id);
321-
const results = await deleteItems([items]);
320+
const item = (await findById(id)) || null;
321+
const results = await deleteItems([item]);
322322
return resolveRefs(results[0]);
323323
};
324324

325325
const deleteOne = async (query) => {
326-
const items = await findOne(query);
327-
const results = await deleteItems([items]);
326+
const item = (await findOne(query)) || null;
327+
const results = await deleteItems([item]);
328328
return resolveRefs(results[0]);
329329
};
330330

331331
const deleteMany = async (query) => {
332332
const items = await findItems(query);
333-
const results = await deleteItems(items);
334-
return resolveRefs(results);
333+
if (items && items.length > 0) {
334+
const results = await deleteItems(items);
335+
return resolveRefs(results);
336+
}
337+
return [];
335338
};
336339

337340
// Helper to wrap public read API with pre/post read middleware

test/lib/model.test.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('model.post', () => {
364364
});
365365

366366
describe('model.pre (read)', () => {
367-
it('pre read middleware csak publikus API-n fusson le', async () => {
367+
it('pre read middleware should run only on public API', async () => {
368368
const preRead = jest.fn();
369369
testModel.pre('read', preRead);
370370
await testModel.find({ name: 'John' });
@@ -595,3 +595,41 @@ describe('model coverage tests', () => {
595595
await expect(m.replaceOne({ _id: a._id }, { name: 'B' })).rejects.toThrow();
596596
});
597597
});
598+
599+
describe('model.deleteOne edge cases', () => {
600+
const userSchema = Schema({
601+
email: { type: 'string', unique: true },
602+
name: { type: 'string' },
603+
});
604+
const userModel = model('useremails', userSchema);
605+
606+
beforeEach(async () => {
607+
await userModel.deleteMany({});
608+
await userModel.insertMany([
609+
{ email: 'a@a.com', name: 'A' },
610+
{ email: 'b@b.com', name: 'B' },
611+
]);
612+
});
613+
614+
it('deleteOne with non-existing email returns null, does not throw', async () => {
615+
const deleted = await userModel.deleteOne({ email: 'notfound@a.com' });
616+
expect(deleted).toBeNull();
617+
const all = await userModel.find({});
618+
expect(all.length).toBe(2);
619+
});
620+
621+
it('deleteMany with non-existing email returns empty array, does not throw', async () => {
622+
const deleted = await userModel.deleteMany({ email: 'notfound@a.com' });
623+
expect(Array.isArray(deleted)).toBe(true);
624+
expect(deleted.length).toBe(0);
625+
const all = await userModel.find({});
626+
expect(all.length).toBe(2);
627+
});
628+
629+
it('findByIdAndDelete with non-existing id returns null, does not throw', async () => {
630+
const deleted = await userModel.findByIdAndDelete('notfoundid');
631+
expect(deleted).toBeNull();
632+
const all = await userModel.find({});
633+
expect(all.length).toBe(2);
634+
});
635+
});

0 commit comments

Comments
 (0)