H
H
hackuun2018-05-30 23:49:24
MongoDB
hackuun, 2018-05-30 23:49:24

How to store the absence of a value in Monge if populations are used?

In the Mongoose model, one of the values ​​looks like this

image: { type: mongoose.Schema.Types.ObjectId, ref: 'Image' }

Those. ObjectId is stored in the database, which points to an object from another collection.
What if this field is not required?
If I store null, then I'm at a dead end on the front how to handle a bunch of conflicts (the data is used in SPA in a dozen components and knocks out an error on the client Cannot read property 'slug' of null
like If I store false, then nothing flies from the API at all, an error like
Cast to ObjectId failed for value \"false\" at path \"_id\" for model \"Image\"

It would be a good option with undefined, but I can't send it undefinedfrom the client - an object with this value is simply ignored and is not sent even by Ajax to the server.
What to do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question