Mongoose validation error - what is it telling me?

54
December 17, 2017, at 4:23 PM

I'm trying to use a JSON file to populate my DB - I've had success with some other files, but this file / model is throwing validation errors and the errors aren't very helpful to me - They say there is a validation error, they even kinda tell me that it's at a forEach line in the schematype.js file - but I'm not gonna go and change a file in the node-modules folder...especially when I think it's my coding fault.

"mongoose": "4.9.8"

This is in the APP.JS - which calls /loaddata:

app.get('/api/loadData', (req, res) => {
    var Xs = require( './_mock_data/x.json' );
    for ( var i in Xs ) {
        X.create( Xs[i] );
        console.log( Xs[i] );
    };
});

This is in my model page:

const XSchema = mongoose.Schema({
    name:{
        type: String,
        required: true
    },
    state:{
        type: String
    },
    type:{
        type: String
    },
    people:{
        type: [mongoose.Schema.Types.Mixed]
    },
    create_date:{
        type: Date,
        default: Date.now
    }
});

My JSON file looks like this - I think the offending data is the "people" with a Mixed type - my confusion comes with the fact that it IS an array of people, so the forEach should loop it - why is it failing?

[
    {
        "name": "Building One",
        "state": "Alaska",
        "people": [{
            "position": "Chair",
            "department": "Sales",
            "email": "aftcs@blah.io",
            "phone": "",
            "name": "John Smith"
        }],
        "type": "Business"
    }, {
        "name": "Fairbanks Folly",
        "state": "Maine",
        "people": [{
            "position": "Sitting",
            "department": "AAA",
            "email": "ffjda@uafert.com",
            "phone": "",
            "name": "Jane Aspnes"
        }, {
            "position": "Standing",
            "department": "BBB",
            "email": "jmbicigo@Maine.com",
            "phone": "",
            "name": "James Bicigo"
        }, {
            "position": "Lying",
            "department": "AAA",
            "email": "kmbutlerhopkins@Craf.org",
            "phone": "",
            "name": "Kam Butler-Hopkins"
        }],
        "type": "Other"
    }
]

And lastly - the ERROR -

Unhandled rejection ValidationError: College validation failed
    at new ValidationError (C:\MyProject\node_modules\mongoose\lib\error\validation.js:23:11)
    at model.Document.invalidate (C:\MyProject\node_modules\mongoose\lib\document.js:1629:32)
    at C:\MyProject\node_modules\mongoose\lib\document.js:1501:17
    at validate (C:\MyProject\node_modules\mongoose\lib\schematype.js:733:7)
    at C:\MyProject\node_modules\mongoose\lib\schematype.js:778:11
    at Array.forEach (<anonymous>)
    at SchemaString.SchemaType.doValidate (C:\MyProject\node_modules\mongoose\lib\schematype.js:738:19)
    at C:\MyProject\node_modules\mongoose\lib\document.js:1499:9
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)

Again, any help and insight is much appreciated!

READ ALSO
Separate Node and React App: Allow Express passthrough to React Router

Separate Node and React App: Allow Express passthrough to React Router

I have two separate apps: one of them is my API in Node with Express, and the other is my front end in React with React Router

62
PDF is uploaded but blank

PDF is uploaded but blank

I'm trying to upload a PDF file to AWS using the request data I'm gettingThis is what the data looks like

68
Mongodb conditionally update doubly nested array

Mongodb conditionally update doubly nested array

My collection looks like thisA document contains an array of suggestions

67