LearnBoost/mongoose — Issue #762: Cast to undefined_method failed for value
Author: Contra
Created: 2012-03-06 06:14:46+00:00
State: open

I'm getting this error when trying to save a MediaPlan object. My code is as follows (excuse the coffee-script):

### MediaPlan

```
Placement = mongoose.model 'Placement'
MediaPlan = new Schema
name:
type: String
required: true

placements: [Placement]
```

### Placement

```coffee-script
Creative = mongoose.model 'Creative'
Publisher = mongoose.model 'Publisher'

Placement = new Schema
name:
type: String
required: true

statistics:
impressions:
type: Number
default: 0
clicks:
type: Number
default: 0
cost:
type: Number
default: 0

publisher: [Publisher]
creative: [Creative]
```
### Publisher

```coffee-script
Publisher = new Schema
name:
type: String
required: true
```

### Creative

```coffee-script
Creative = new Schema
name:
type: String
required: true
```

### Usage

```coffee-script
pm =
name: 'spot1'
statistics:
impressions: 433
clicks: 30
cost: 0.01
publisher: [
name: 'CNN'
]
creative: [
name: 'Pork N Beans Ad 30'
]

opm =
name: 'spotabc'
statistics:
impressions: 433
clicks: 30
cost: 0.01
publisher: [
name: 'New York Times'
]
creative: [
name: 'Pork N Beans Ad 32'
]

mp = new MediaPlan
name: "Cowboy Rocket Fuel Summer Slam"
placements: [pm, opm]

mp.save (err, mp) ->
console.log err
```
### Full error

```
{ message: 'Cast to undefined_method failed for value "[object Object],[object Object]"',
name: 'CastError',
type: 'undefined_method',
value: [ [object Object], [object Object] ] }
```

I am loading the models into mongoose via mongoose.model but left out that code for cleanliness. Am I using embedded documents wrong or is this an issue with mongoose itself? The problem is located in the MediaPlan.placements field.