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.

Schema, array, default value (default value "[]" is not working)

See original GitHub issue
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/dbTest');

var arrayTestSchema = new Schema({
    anArray: {
      type: Array,
      'default': []
    }
});

mongoose.model('ArrayTest', arrayTestSchema);
ArrayTest = mongoose.model('ArrayTest');

var myTest = new ArrayTest();
console.log(arrayTestSchema.anArray);

mongoose.connection.close();

displays “undefined”

Issue Analytics

  • State:closed
  • Created 13 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
bnoguchicommented, Feb 16, 2011

It’s failing because you’re not accessing your document instance. I’ve commented out your console.log and replaced it with the correct one.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/dbTest');

var arrayTestSchema = new Schema({
    anArray: {
      type: Array,
      'default': []
    }
});

mongoose.model('ArrayTest', arrayTestSchema);
ArrayTest = mongoose.model('ArrayTest');

var myTest = new ArrayTest();
// console.log(arrayTestSchema.anArray);
console.log(myTest.anArray);

mongoose.connection.close();
0reactions
raysk4evercommented, Oct 21, 2019

@bnoguchi what about the type of array items?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't my schema to add default values in mongoose ...
Default values really don't work with arrays, unless of course it is a document within the array and you want to set a...
Read more >
Default value for nested arrays - Working with Data - MongoDB
is it possible in MongoDB to have default values in nested arrays or objects? I use the following schema for a nested object:....
Read more >
Specify default column values | BigQuery - Google Cloud
You can compose a STRUCT or ARRAY default value with these functions, ... The default value for an array cannot be NULL or...
Read more >
Mongoose v6.8.1: Defaults
Your schemas can define default values for certain paths. ... Note: Mongoose only applies a default if the value of the path is...
Read more >
Initial Value Templates - Sanity.io
By default, whenever you create a new document in the studio, ... project schema type is given the value false for the isHighlighted...
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