2017-11-01 12 views
0

Ich habe Probleme mit dem Express-Validator, insbesondere die isDate-Funktion. Ich habe Schritte unternommen, um expressvalidator, bodyparse, validator-Modul usw. Alle Routen sind nur danach. Die Umgebung ist Node + Express.TypeError: req.checkBody (...). Optional (...). IsDate ist keine Funktion

Das Problem ist auf der Verwendung von

"req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate();" 

und ich halte die folgenden Fehler bekommen.

TypeError: req.checkBody(...).optional(...).isDate is not a function 
    at exports.author_create_post (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/controllers/authorController.js:47:81) 
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5) 
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5) 
    at /Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:281:22 
    at Function.process_params (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12) 
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10) 
    at Function.handle (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:174:3) 
    at router (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:47:12) 
    at Layer.handle [as handle_request] (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/layer.js:95:5) 
    at trim_prefix (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:317:13) 
    at /Users/svitaworld/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:284:7 
    at Function.process_params (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:335:12) 
    at next (/Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:275:10) 
    at /Users/mycomputer/Desktop/NodeJS/express-locallibrary-tutorial/node_modules/express/lib/router/index.js:635:15 

app.js

app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ 
    extended: false 
})); 
app.use(expressValidator()); // Add this after the bodyParser middlewares! 
app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.use('/', index); 
app.use('/users', users); 
app.use('/catalog', catalog); // Add catalog routes to middleware chain. 

in einem meiner Controller, ich bin die isDate() Methode verwendet eine Validierung Datum zu tun, die ich separat definiert haben int er AuthorSchema.

var AuthorSchema = Schema( { 
    first_name: {type: String, required: true, max: 100}, 
    family_name: {type: String, required: true, max: 100}, 
    date_of_birth: {type: Date}, 
    date_of_death: {type: Date}, 
}); 

nun die Post-Anfragen behandeln ich diesen Code in der Steuerung haben:

authorController.js - Linie von der Linie 41-48

// Handle Author create on POST 
exports.author_create_post = function(req, res, next) { 
    console.log("DEBUG: starting in exports.author_create_post"); 
    req.checkBody('first_name', 'First name must be specified.').notEmpty(); 
    req.checkBody('family_name', 'Family name must be specified.').notEmpty(); 
    req.checkBody('family_name', 'Family name must be alphanumeric text.').isAlpha(); 
    req.checkBody('date_of_birth', 'Invalid date').optional({ checkFalsy: true }).isDate(); // Error is on this usage of isDate() 
    req.checkBody('date_of_death', 'Invalid date').optional({ checkFalsy: true }).isDate(); 

Antwort

1

isDate() wurde von Validator entfernt. js. Sie können this Commit auf GitHub für weitere Informationen sehen. Express-Validator verwendet validator.js für die Validierung.

Sie können einen benutzerdefinierten Validator erstellen, um nach gültigen Daten zu suchen. Für die neue API:

check('date').custom(isValidDate).withMessage('the date must be valid'); 

für den Legacy-API:

app.use(expressValidator({ 
    customValidators: { 
    isValidDate: isValidDate 
    } 
})); 

, wenn Sie die Middleware (in app.js oder so ähnlich) und zur Überprüfung gelten:

req.checkBody('date', 'the date must be valid').isValidDate(); 

isValidDate() muss von dir selbst geschrieben werden. Hier ein Beispiel:

function isValidDate(value) { 
    if (!value.match(/^\d{4}-\d{2}-\d{2}$/)) return false; 

    const date = new Date(value); 
    if (!date.getTime()) return false; 
    return date.toISOString().slice(0, 10) === value; 
} 

Diese prüft, ob Daten mit dem yyyy-mm-dd Format. Es wurde von this Antwort genommen. Es gibt auch viele andere Antworten für verschiedene Formate hier auf Stack-Überlauf:

Oder Moment isValid() des verwenden.

+0

Vielen Dank. Ich schätze, ich werde Moment isValid() als Ersatz verwenden. var dateFormat = "TT/MM/JJJJ"; Moment (req.body.date_of_birth, dateFormat, true) .isValid(); –

Verwandte Themen