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.

Empty array is saved when a property references a schema

See original GitHub issue
var FooSchema = new Schema({});
var Foo = mongoose.model("Foo", FooSchema);
var BarSchema = new Schema({
    foos: [Foo.schema]
});
var Bar = mongoose.model("Bar", BarSchema);

var b = new Bar();
b.save();

That will create an empty array of b.foos instead of just leaving the property undefined.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:1
  • Comments:39 (2 by maintainers)

github_iconTop GitHub Comments

42reactions
vkarpov15commented, Oct 7, 2016

@antonioaltamura I forgot about this issue, the below works in mongoose 4.6:

const CollectionSchema = new Schema({
 field1: { type: [String], default: void 0 }, // <-- override the array default to be undefined
});

const Collection = mongoose.model('test', CollectionSchema);

Collection.create({}).
  then(doc => { console.log(doc); return doc; }).
  then(() => { process.exit(0); });
32reactions
ifeltsweetcommented, Feb 10, 2016

Sorry, but this is a horrible design. You should not be deciding on my behalf wether I want it to be an empty array or not set at all. At least provide an option to disable this feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to save an empty array as property of a nested object in ...
The usual request is to "supress" that. If you get no array, then your schema is not actually what you think it is....
Read more >
empty - Manual - PHP
I'm summarising a few points on empty() with inaccessible properties, in the hope of saving others a bit of time. Using PHP 5.3.2....
Read more >
Configuring the Map policy in the user interface - IBM
If you select Object or Array, you can create a schema through the user interface after you have clicked Done and returned to...
Read more >
Work with arrays | BigQuery - Google Cloud
Flattening arrays with a CROSS JOIN excludes rows that have empty or NULL arrays. ... In this example, each value pair is stored...
Read more >
Semi-structured Data Types - Snowflake Documentation
We often refer to these data types as semi-structured data types. ... the first of which is an empty ARRAY, and the second...
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