Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
narf opened this issue Feb 15, 2011 · 6 comments
Closed

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

narf opened this issue Feb 15, 2011 · 6 comments

Comments

@narf
Copy link

narf commented Feb 15, 2011

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"

@lukegalea
Copy link

At least in master, this works fine for me.. except it returns a MongooseArray so it's full of lots of other craziness :) Not just the value.

What version are you running?

@pradeepthundiyil
Copy link

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

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

var arrayTestSchema = new Schema({

 Title: String,
 Description: String,
 Status: String

});

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

var myTest = new ArrayTest();

myTest.Title = 'Project';
myTest.Description = 'using mongodb & node';
myTest.Status = 'open';

console.log(myTest);

mongoose.connection.close();

@narf
Copy link
Author

narf commented Feb 16, 2011

I'm running 1.0.12, npm version.
Should be the master version as I see that the 1.0.12 was released yesterday.

Don't understant what you want to point out pradeepthundiyil.

@bnoguchi
Copy link
Contributor

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();

@narf
Copy link
Author

narf commented Feb 17, 2011

lol, ok i see, yesterday was a bad day for me. Thanks for pointing this out =)

@raysk4ever
Copy link

@bnoguchi what about the type of array items?

@Automattic Automattic locked as resolved and limited conversation to collaborators Oct 25, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants