Skip to content
Advertisement

Don’t store keys with empty string values in mongodb document

i would like to store a post as a document in mongodb. I’m using mongoose for modelling and the content is created by a user using a form. The content of the form is append to FormData and sending to server. This works so far. The only issue is, that empty fields, that are appended as empty strings in the req.body will be stored in the document. The minimalize-property of my dataschema is already set true …

JavaScript

req.body looks like:

JavaScript

My document looks exactly the same, but i would like to make it look like this:

JavaScript

Thanks so much for your help!

Advertisement

Answer

You could build an object by filtering the req.body empty properties with:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement