Mongoose document save fails with "TypeError: Cannot read property 'options' of undefined"

I have a mongoose schema with a few "ref" properties. Recently (I believe after upgrading to a recent version of mongoose), my saves have started failing unless I populate() the ref fields. The error it fails with is

TypeError: Cannot read property 'options' of undefined

Has anyone else seen this? I suppose I can work around it by always populating, but I'd like to understand why this is necessary.

This was caused by my custom toObject and toJSON transform, which was removing properties I don't want serialized when returning JSON through my API.

I didn't realize it also affected Mongoose's serialization to MongoDB.

Edit: it can also be caused by trying to save a document with 'ref' properties which were not populated!

I had a similar issue and it helped to send the HTTP header field Content-Type: application/json which was previously missing. Adding this helped.