question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

avoid empty object/array creation

See original GitHub issue

Hi, I find in my documents some fields like:

   ...
   my_array_of_obj_field: [{}],
   my_obj_field: {},
   my_str_field: "",

    ...

(the cause is angularjs in frontend that initializes the empty model)

what’s the best way to avoid storing of those fields?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

30reactions
vkarpov15commented, Jun 1, 2016
  • If you have an array in your schema, mongoose will create an empty array [] for you by default so you can do .push() and such without explicitly creating the array. To shut this off, overwrite the default array default (requires mongoose 4.4.15 or higher)
var schema = new Schema({
  myArr: { type: [String], default: undefined }
});
  • Empty objects shouldn’t happen because of the minimize property, unless you explicitly set minimize: false.
  • For strings, set required: true and empty strings will no longer be allowed.
0reactions
vkarpov15commented, Jun 2, 2019

The undefined to null is expected behavior unfortunately, see comments here. Will fix the lowercase issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I remove empty object in from an array in JS
You can use Array.prototype.filter to remove the empty objects before stringifying. JSON ...
Read more >
477780 – Avoid creating an empty array via new Object[0];
I frequently see in our code base the creation of an empty array via new Object[0]; We should avoid this unnecessary object creation,...
Read more >
Empty Arrays - MATLAB & Simulink - MathWorks
Once you assign a nonempty object to an array, all array elements must be nonempty objects. Note. A class constructor must avoid returning...
Read more >
TypeError: Reduce of empty array with no initial value
The JavaScript exception "reduce of empty array with no initial value" occurs when a reduce function is used. Message. TypeError: Reduce of empty...
Read more >
remove empty object from array of objects javascript - You.com
Note: To remove both empty objects and empty arrays from the array, you can shorten the comparison to Object.keys().length === 0, as arrays...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found