Cast error inserting array (embedded documents) using Mongoose?

3,331 views
Skip to first unread message

Ryan Kelly

unread,
Jan 30, 2013, 9:59:53 PM1/30/13
to nod...@googlegroups.com
I have tried multiple ways to do this, all resulting in an error. I have checked, and double checked Mongoose documentation and examples so I must be doing something wrong. I want to be able to insert 1-to-many contacts (Contact.js) into an array of contacts ([Contact]) for a given venue (Venue.js).

It seems like I should be able to do this:

// contact model
var mongoose = require('mongoose')
   ,Schema = mongoose.Schema
   ,ObjectId = Schema.ObjectId;
 
var contactSchema = new Schema({    
  firstName  : { type: String, required: true, trim: true },
  lastName  : { type: String, required: true, trim: true },
email    : { type: String, required: true, trim: true },
created : {type: Date, default: Date.now}
});
 
module.exports = mongoose.model('Contact', contactSchema);


// venue model
var Contact = require('./contact.js'); 
var mongoose = require('mongoose')
   ,Schema = mongoose.Schema;

var venueSchema = new Schema({   
_id     : { type: String, required: true, trim: true },
name     : { type: String, required: true, trim: true },  
description     : { type: String, required: true, trim: true },
market : { type: String, required: true, trim:true }, // key to Market.js
state : { type: String, required: true, trim:true }, // key to State.js
isActive : { type: Boolean, required: false, default: false },
siteUrl     : { type: String, required: false, trim: true },
contacts : [{ type: Contact, required: true, trim:true }], // array of Contact.js
privateNotes    : { type: String, required: false, trim: true },   
    comments : { type: String, required: false, trim: true },
    coverImage : { type: String, required: true, trim: true },
    images : { type: String, required: false, trim: true }
});
 
module.exports = mongoose.model('Venue', venueSchema);


// venues.js
exports.addVenue = function(req, res) {
  var v = req.params;

  venueExists(v, function(exists) {
      if (exists) { 
        return res.send(new restify.ConflictError("Venue already exists"));
      } else {
        var venue = new Venue({
          _id      : string(v.name).slugify().s, // need to replace space with _ and make lowercase
          name          : v.name,  
          description   : v.description,
          isActive      : v.isActive,
          siteUrl       : v.siteUrl,
          market        : v.market,       // key to Market.js
          state         : v.state,        // key to State.js
          contacts      : v.contacts      // array of objects (Contact.js)
          privateNotes  : v.privateNotes,   
          comments      : v.comments,
          coverImage    : v.coverImage,
          images        : v.images
        });

        venue.save(function (err) {
          if (!err) {
            return res.send(201, venue);
          } else {
            return res.send(err);
          }
        });
      }
  });
}

It returns a casting error:
"message": "Cast to undefined_method failed for value \"[object Object],[object Object]\" at path \"contacts\""

any thoughts?

Ryan Kelly

unread,
Jan 30, 2013, 10:08:12 PM1/30/13
to nod...@googlegroups.com
Here is the sample payload I'm passing in:

{
  "name":"my venue",
  "description":"my description",
  "isActive":true,
  "siteUrl":"http://google.com",
  "market":"resort",
  "state":"CO",
  "contacts": [
        {
            "firstName": "Mike",
            "lastName": "Ditka",
          "email": "mi...@gmail.com"
        },
        {
            "firstName": "Dick",
            "lastName": "Butkus",
          "email": "di...@gmail.com"
        }
    ],
  "privateNotes":"my notes",
  "comments":"my comments",
  "coverImage":"default.png",
  "images":"myImage.png"
}

Daniel Rinehart

unread,
Jan 31, 2013, 10:30:09 AM1/31/13
to nodejs
Some things to try:
1) In order to treat a contact as an embedded document in venue you need to export the contact schema from contact model, not the model
2) In venue model you would specify an array of embedded contact documents using "contacts: [contactSchema]"

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ryan Kelly

unread,
Jan 31, 2013, 12:00:28 PM1/31/13
to nod...@googlegroups.com
that worked! I swear I thought I tried that but apparently not. Thanks Daniel!

Here is my new venue model.

// venue model 
var mongoose = require('mongoose')
   ,Schema = mongoose.Schema;

var contactSchema = new Schema({    
  firstName  : { type: String, required: true, trim: true },
  lastName  : { type: String, required: true, trim: true },
email  : { type: String, required: true, trim: true },
created  : {type: Date, default: Date.now}
});


var venueSchema = new Schema({   
_id     : { type: String, required: true, trim: true },
name     : { type: String, required: true, trim: true },  
description     : { type: String, required: true, trim: true },
market : { type: String, required: true, trim:true }, // key to Market.js
state : { type: String, required: true, trim:true }, // key to State.js
isActive : { type: Boolean, required: false, default: false },
siteUrl     : { type: String, required: false, trim: true },
contacts : [contactSchema], // array of Contact.js
Reply all
Reply to author
Forward
0 new messages