Question

I'm having a strange problem with Mongoose not honoring the schema I've defined. I am passing data that properly conforms to the specified schema, however I'm either encountering cast errors, or the data (when I exclude the information that fails to cast) is ending up in mongo as the wrong datatypes.

The schema is as follows:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var AccountSchema = new Schema({
    userId: { type: Number, unique: true, dropDups: true },
    referralCode: String,
    dateStarted: { type: Date, default: Date.now },
    accountType: Number,
    accountCategories: [String],
    beneficiaries: [{
        id: Number,
        relationshipType: Number,
        percentage: Number,
        firstName: String,
        lastName: String,
        middleInitial: String,
        mailingAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        },
        ssn: String,
        birthDate: Date,
        isPrimary: Boolean
    }],
    accountOwner: {
        firstName: String,
        lastName: String,
        middleInitial: String,
        birthDate: Date,
        ssn: String,
        phoneNumber: String,
        mailingAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        },
        physicalAddress: {
            address1: String,
            address2: String,
            address3: String,
            city: String,
            state: String,
            country: String,
            postalCode: String
        }
    },
    fundsTransfer: {
        bankName: String,
        type: Number,
        transferType: Number,
        routingNumber: String,
        accountNumber: String,
        currentYearAmount: Number,
        previousYearAmount: Number
    },
    accountTransfer: {
        accountType: Number,
        custodianName: String,
        accountNumber: String,
        phoneNumber: String,
        faxNumber: String,
        cashAmount: Number,
        cashPortionOptions: Number,
        expediteOption: Boolean,
        otherAccountType: String,
        planType: Number
    },
    wizardFlags: {
        doCashContrib: Boolean,
        doCashTransfer: Boolean,
        doCashContribAndTransfer: Boolean,
        doAssetTransfer: Boolean
    }
});

var Account = mongoose.model("Account", AccountSchema);

module.exports = Account;

The data being passed in is as follows:

this.mockAccountState = {
    userId: 9,
    referralCode: "",
    dateStarted: Date.now(),
    accountOwner: {
        firstName: "Test",
        lastName: "User",
        middleInitial: "X",
        birthDate: null,
        ssn: "123-45-6789",
        phoneNumber: "(123) 456-7890",
        mailingAddress: {
            address1: "123 A Street",
            address2: "",
            address3: "",
            city: "A City",
            state: "CO",
            country: "US",
            postalCode: "12345"
        },
        physicalAddress: null
    },
    accountType: 0,
    accountCategories: [],
    fundsTransfer: {
        bankName: "",
        type: 0,
        transferType: 1,
        routingNumber: "123456789",
        accountNumber: "111222333444",
        currentYearAmount: 6000,
        previousYearAmount: 0
    },
    assetTransfer: {},
    hasBeneficiaries: false,
    beneficiaries: [],
    wizardFlags: {
        doCashContrib: true,
        doCashTransfer: false,
        doCashContribAndTransfer: false,
        doAssetTransfer: false
    }
};

If I pass in a non-empty string value for bankName, I get the following error:

Error updating account: CastError: Cast to number failed for value "Bank of Test" at path "fundsTransfer"

If I exclude bankName, then the data that actually ends up in the database is as follows:

enter image description here

If I select this data using the mongo shell, I get:

{ 
    "_id" : ObjectId("536ae87342d6347028b42e1e"),
    "userId" : 1, 
    "referralCode" : "", 
    "accountType" : 1, 
    "wizardFlags" : { 
        "doAssetTransfer" : false, 
        "doCashContribAndTransfer" : false, 
        "doCashTransfer" : false, 
        "doCashContrib" : true
    }, 
    "accountOwner" : { 
        "mailingAddress" : { 
            "postalCode" : "12345", 
            "country" : "US",
            "state" : "CO",
            "city" : "A City", 
            "address3" : "", 
            "address2" : "", 
            "address1" : "123 A Street" 
        }, 
        "physicalAddress" : {  }, 
        "phoneNumber" : "(123) 456-7890",
        "ssn" : "123-45-6789",
        "birthDate" : null,
        "middleInitial" : "X",
        "lastName" : "User",
        "firstName" : "Test"
    }, 
    "beneficiaries" : [ ], 
    "accountCategories" : [ "3" ],
    "dateStarted" : ISODate("2014-05-08T02:14:05.379Z"),
    "__v" : 0,
    "accountTransfer" : {  }, 
    "fundsTransfer" : { 
        "bankName" : null,
        "type" : 0, 
        "transferType" : 1, 
        "routingNumber" : 123456789, 
        "accountNumber" : 111222333444,
        "currentYearAmount" : 6000, 
        "previousYearAmount" : 0 
    }
}

However the data that Mongoose actually returns is:

{
    "dateStarted":"2014-05-08T02:14:05.379Z",
    "accountCategories":["3"],
    "beneficiaries":[],
    "accountOwner":{
        "mailingAddress":{
            "address1":"123 A Street",
            "address2":"",
            "address3":"",
            "city":"A City",
            "state":"CO",
            "country":"US",
            "postalCode":"12345"
        },
        "physicalAddress":{},
        "firstName":"Test",
        "lastName":"User",
        "middleInitial":"X",
        "birthDate":null,
        "ssn":"123-45-6789",
        "phoneNumber":"(123) 456-7890"
    },
    "accountTransfer":{},
    "wizardFlags":{
       "doCashContrib":true,
       "doCashTransfer":false,
       "doCashContribAndTransfer":false,
       "doAssetTransfer":false
   },
   "__v":0,
   "accountType":1,
   "referralCode":"",
   "userId":1,
   "_id":"536ae87342d6347028b42e1e"
}

The fundsTransfer child object isn't even present, even though it is definitely in mongodb.

I'm quite confused. I don't understand why the rest of the schema works, but the fundsTransfer object simply refuses to work. The data and the schema match, something somewhere, I am quit

Was it helpful?

Solution

In AccountSchema, you need to change the type field definition from:

type: Number,

to

type: {type: Number},

Otherwise, Mongoose thinks you're defining the type of the parent fundsTransfer object as a Number instead of defining a field named 'type' within it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top