Re: [mongoose] Re: Nested document gets a cast error since 4.0

54 views
Skip to first unread message

Ryan Wheale

unread,
Jun 16, 2015, 2:58:44 PM6/16/15
to mongoo...@googlegroups.com
This has bit me before. The least amount of information mongoose needs to define a property is the "type" - making these two property definitions analogous:

   name: String,
   email: { type: String }
}

... but as you discovered creating a "type" field on your entity is a bit confusing to mongoose. It's a wonder that "type" isn't part of Schema.reserved.

On Thu, Jun 11, 2015 at 12:41 PM, Jean-François St-Germain <jf.stg...@gmail.com> wrote:
Argh the nested doc "card" has a "type" field which gets confused with the mongoose field definition type keyw!


On Thursday, 11 June 2015 14:09:49 UTC-4, Jean-François St-Germain wrote:
Hello,

There's a problem I've been trying to troubleshoot without any luck so far.  We're in the progress of migrating from mongoose 3.6.11 to 4.0.5 and trying to create a model instance with a nested document results in a validation error :
{
     
"message": "User validation failed",
     
"name": "ValidationError",
     
"errors": {
       
"card": {
         
"message": "Cast to String failed for value \"[object Object]\" at path \"card\"",
         
"name": "CastError",
         
"kind": "String",
         
"value": {
           
"holderName": "test-0",
           
"type": "test-0",
           
"number": "test-0",
           
"ccvCode": "test-0",
           
"expiryDate": "test-0",
           
"billing": {
             
"city": "test-0",
             
"state": "test-0",
             
"zip": "test-0",
             
"country": "test-0",
             
"phone": "test-0"
           
}
         
},
         
"path": "card"
       
}
     
}
   
}

I'm basically creating a model instance doing:
user = new options.db.models.User(plainJsUserObject);

Here's the schema definition (see the nested doc at field "card"):

UserSchema = new mongoose.Schema({
  username
: {
    type
: String,
    unique
: true,
    lowercase
: true,
    trim
: true,
    index
: true
 
},
  password
: {
    type
: String,
    validate
: [/(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{10,}/, "invalid_password"],
    required
: true
 
},
  loginAttempts
: {
    type
: Number,
    required
: true,
   
"default": 0
 
},
  lockUntil
: {
    type
: Number
 
},
  blocked
: Boolean,
  firstName
: String,
  lastName
: String,
  nameLower
: {
    type
: String,
    index
: true
 
},
  address
: String,
  city
: String,
  state
: String,
  zip
: String,
  country
: String,
  phone
: String,
  email
: {
    type
: String,
    validate
: [/\S+@\S+\.\S/, 'invalid_email'],
    required
: true,
    lowercase
: true,
    trim
: true,
    unique
: true,
    index
: true
 
},
  card
: {
    holderName
: String,
    type
: String,
    number
: String,
    ccvCode
: String,
    expiryDate
: String,
    billing
: {
      city
: String,
      state
: String,
      zip
: String,
      country
: String,
      phone
: String
   
}
 
}
});




When I'm inspecting the schema definition after it has been registered, it seems like the "card" nested document has its type defined as String instead of Object:
// ...
 card
:
 
{ enumValues: [],
    regExp
: null,
    path
: 'card',
    instance
: 'String',
    validators
: [],
    setters
: [],
    getters
: [],
    options
: [Object],
    _index
: null },
// ...

Any idea?

--
Documentation - http://mongoosejs.com/
Plugins - http://plugins.mongoosejs.com/
Bug Reports - http://github.com/learnboost/mongoose
Production Examples - http://mongoosejs.tumblr.com/
StackOverflow - http://stackoverflow.com/questions/tagged/mongoose
Google Groups - https://groups.google.com/forum/?fromgroups#!forum/mongoose-orm
Twitter - https://twitter.com/mongoosejs
IRC - #mongoosejs
---
You received this message because you are subscribed to the Google Groups "Mongoose Node.JS ODM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-orm...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages