Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

default subdoc array #953

Closed
pgherveou opened this Issue · 3 comments

3 participants

@pgherveou

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)
@aheckmann
Owner

thanks for the report. looking into it

@aheckmann aheckmann closed this issue from a commit
@aheckmann aheckmann fixed; setting default DocumentArrays
closes #953

Conflicts:

	lib/document.js
	lib/types/documentarray.js
17ff241
@aheckmann aheckmann closed this in 17ff241
@aheckmann aheckmann referenced this issue from a commit in aheckmann/mongoose
@aheckmann aheckmann fixed; setting default DocumentArrays
closes #953
6f4e0fe
@TimNZ

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??

@aheckmann
Owner

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

@aheckmann aheckmann referenced this issue from a commit
@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 referenced this issue from a commit
@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
Something went wrong with that request. Please try again.