Skip to content

Commit ed79a5a

Browse files
authoredSep 23, 2024
Merge pull request #14901 from Automattic/IslandRhythms/docs-fix
Update migrating_to_6.md about lodash `isEmpty()`
2 parents becd799 + f86bda7 commit ed79a5a

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
 

‎docs/migrating_to_6.md

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ If you're still on Mongoose 4.x, please read the [Mongoose 4.x to 5.x migration
5252
* [`toObject()` and `toJSON()` Use Nested Schema `minimize`](#toobject-and-tojson-use-nested-schema-minimize)
5353
* [TypeScript changes](#typescript-changes)
5454
* [Removed `reconnectTries` and `reconnectInterval` options](#removed-reconnecttries-and-reconnectinterval-options)
55+
* [Lodash `.isEmpty()` returns false for ObjectIds](#lodash-object-id)
5556

5657
<h2 id="version-requirements"><a href="#version-requirements">Version Requirements</a></h2>
5758

@@ -541,3 +542,20 @@ The `reconnectTries` and `reconnectInterval` options have been removed since the
541542

542543
The MongoDB node driver will always attempt to retry any operation for up to `serverSelectionTimeoutMS`, even if MongoDB is down for a long period of time.
543544
So, it will never run out of retries or try to reconnect to MongoDB.
545+
546+
547+
<h2 id="lodash-object-id"><a href="#lodash-object-id">Lodash <code>.isEmpty()</code> returns true for ObjectIds</h2>
548+
549+
Lodash's `isEmpty()` function returns true for primitives and primitive wrappers.
550+
`ObjectId()` is an object wrapper that is treated as a primitive by Mongoose.
551+
But starting in Mongoose 6, `_.isEmpty()` will return true for ObjectIds because of Lodash implementation details.
552+
553+
An ObjectId in mongoose is never empty, so if you're using `isEmpty()` you should check for `instanceof ObjectId`.
554+
555+
```javascript
556+
if (!(val instanceof Types.ObjectId) && _.isEmpty(val)) {
557+
// Handle empty object here
558+
}
559+
```
560+
561+

0 commit comments

Comments
 (0)
Please sign in to comment.