2017-11-01 28 views
0

エクスプレスバリデータを使用して問題が発生しています。具体的にはisDate関数です。私はexpressvalidator、ボディーセレクト、バリデーターモジュールなどを使用するための措置を講じています。すべてのルートはこの後になります.. 環境はNode + Expressです。TypeError:req.checkBody(...)。オプション(...)。isDateが関数ではありません。

問題が

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

の使用状況にあると私は、次のエラーを取得しておきます。

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. 

、私は彼がAuthorSchema int型私は、別に定義した日に、いくつかの検証を行うためにISDATE()メソッドを使用しています。今

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}, 
}); 

私はコントローラで、このコード持ってPOSTリクエスト処理する:

authorController.jsを - ラインからライン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(); 

答えて

1

isDate()はバリデータから削除されました。 js。詳細については、this GitHubでのコミットを参照してください。 express-validatorは検証のためにvalidator.jsを使用します。

カスタムバリデータを作成して有効な日付を確認することができます。

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

レガシーAPIの場合:新しいAPIについては

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

あなたは(app.jsまたは類似した何かで)ミドルウェアを適用し、検査のため:

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

isValidDate()自分で書かなければなりません。 YYYY-MM-DD形式の日付の

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; 
} 

このチェック:ここでは例があります。それはthis答えから取られました。異なるフォーマットのための他の回答の多くは、ここでは、スタックオーバーフローのもあります

やモーメントのisValid()を使用しています。

+0

ありがとうございます。私は、代わりに瞬間のisValid()を使用すると思います。 var dateFormat = "DD/MM/YYYY"; moment(req.body.date_of_birth、dateFormat、true).isValid(); –

関連する問題