Saturday 10 June 2017

Mongoose not validating in schema

I'm having trouble getting my schema model to validate. My code seems to simply ignore that there are required and validate type is the mode. I created a custom validator to see if the inserted string is = 'below' . Even if I leave the key blank, it will still post the model into the collection.

//modeljs
    var mongoose = require('mongoose');
    var Schema = mongoose.Schema;

    var directionValidator = [
            function (val) {
                console.log(val); //check 
                return (val.toLowerCase() = 'below')
              },
            'Invalid Direction'
            ];
    var directionSchema = new Schema({

    email: {
    type: String,
    required: true,
    validate: requiredStringValidator
},
module.exports = mongoose.model('direction', directionSchema);




//controllerjs
    var newAlert = require('../models/modeljs');
    var mongoose = require('mongoose');
    mongoose.connect('mongoURL');
    var db = mongoose.connection;

exports.create = function (req, res) {
    var entry = new newAlert({       
        direction: req.body.direction

});

db.collection('direction').insert(entry,function(err){
        if (err) {
            var errMsg = "There was an error saveing" + err;
            res.send(errMsg);
    }
    else{
        console.log('no errors saving');
    }
res.send('All inserted')
};



via rudster

No comments:

Post a Comment