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

Already on GitHub? Sign in to your account

default subdoc array #953

Closed
pgherveou opened this Issue Jun 7, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Contributor

pgherveou commented Jun 7, 2012

the following test fails is there something wrong with this piece of code:

#!/usr/bin/env node

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

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

var childSchema = new Schema({
  name: {type: String}
});

var parentSchema = new Schema({
  name: {type: String, default: 'parent-default'},
  children: {type: [childSchema], default: [{name: 'child-default'}]}
});

var Model = mongoose.model('Parent', parentSchema);

Model.create({}, function(err, m) {
  assert.equal(err, null);
  assert.equal(m.name, 'parent-default');
  assert.deepEqual(m.children, [{name: 'child-default'}]);
  mongoose.disconnect();
});

the stack trace is

TypeError: undefined is not a function
    at DocumentArray.cast (/tests/node_modules/mongoose/lib/schema/documentarray.js:120:20)
    at DocumentArray.cast (/tests/node_modules/mongoose/lib/schema/documentarray.js:131:17)
    at DocumentArray.default (/tests/node_modules/mongoose/lib/schematype.js:53:14)
    at DocumentArray.SchemaType (/tests/node_modules/mongoose/lib/schematype.js:30:13)
    at DocumentArray.SchemaArray (/tests/node_modules/mongoose/lib/schema/array.js:30:14)
    at new DocumentArray (/tests/node_modules/mongoose/lib/schema/documentarray.js:42:13)
    at Function.interpretAsType (/tests/node_modules/mongoose/lib/schema.js:220:14)
    at Schema.path (/tests/node_modules/mongoose/lib/schema.js:185:29)
    at Schema.add (/tests/node_modules/mongoose/lib/schema.js:121:12)
    at new Schema (/tests/node_modules/mongoose/lib/schema.js:39:10)
Collaborator

aheckmann commented Jun 7, 2012

thanks for the report. looking into it

@aheckmann aheckmann closed this in 17ff241 Jun 8, 2012

@aheckmann aheckmann added a commit to aheckmann/mongoose that referenced this issue Jun 9, 2012

@aheckmann aheckmann fixed; setting default DocumentArrays
closes #953
6f4e0fe

TimNZ commented Jan 29, 2013

var WorkflowSchema = new mongoose.Schema({
name: { type: String }
},
{ id: false, _id: false } );

var testSchema = new mongoose.Schema({
status: { type: String, default: 'new' },
_workflow: { type: [WorkflowSchema], default: [ {} ] }
});

.... model stuff

console.log(test);

=>

{ status: 'new',
_workflow: [ undefined ],
workflow_template_id: '7585d9d0e0a84b2cb426f6e57c10d7cd' }

It seems that an empty object and no defaults on any schema fields results in undefined being returned.

Add a default to WorkflowSchema.name, or default: [ { name: 'boo' } ] in testSchema._workflow, and it works fine.

Surely not correct behaviour??

Collaborator

aheckmann commented Feb 14, 2013

@TimNZ fixed in next release. btw, the object is actually there, it is just the console.log output that displays it improperly.

@aheckmann aheckmann added a commit that referenced this issue Feb 15, 2013

@aheckmann aheckmann fixed; display empty docs as {} in log output
schemas with _id and id set to false were output as undefined
instead of {}

follow up to #953
9783335

@aheckmann aheckmann added a commit that referenced this issue Feb 15, 2013

@aheckmann aheckmann fixed; display empty docs as {} in log output
schemas with _id and id set to false were output as undefined
instead of {}

follow up to #953
9516d17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment