Clone object in array to add as new element

232 views
Skip to first unread message

Chris Mcvittie

unread,
Feb 9, 2012, 12:53:22 PM2/9/12
to mongoo...@googlegroups.com
Hi, 
I've an object with an array of schemas.  

e.g.

Address= new Schema(
  Line 1: String
)
Person = new Schema(
  Addresses: [Address]
)

I have a case where I want to clone the last address and add it to the Addresses array.

addr = _.clone(_.last(p.Addresses))
p.Addresses.push(addr)
p.save(...)

but I'm getting the error below when I try to save.
TypeError: Object function toString() { [native code] } has no method 'cast'" 

Is there a better way to do this in mongoose?

Thanks, 
Chris


George Snelling

unread,
Feb 15, 2012, 2:02:54 PM2/15/12
to mongoo...@googlegroups.com
Hi Chris, this is just a guess, but rather than cloning, you might try the casting the document to an ordinary object with the toObject() method, then new up a fresh document based on that object.  Again -- just a guess.  

-g
Reply all
Reply to author
Forward
0 new messages