我創建了一個路由作爲驗證我所有的路由,我希望這能夠處理所有請求並驗證它們,問題是我想檢查屬性是否在請求中,而不是數據只是屬性,讓我們說用戶有一個電子郵件,但其他人沒有,我想檢查身體是否有電子郵件屬性運行某些代碼來驗證此電子郵件。nodejs express mongoose mongodb
我怎麼知道,如果
req.body.email;
在身體?
var express = require('express');
var router = express.Router();
router.use('/', function(req, res, next) {
var record = req.body.record,
email = record.email,
phone_number = record.phone_number,
school_id = req.body.schoolId;
console.log("validator");
if (record) {
if (what is the condition here to check
if the body has email)
req.asyncValidationErrors()
.then(function() {
next();
}).catch(function(errors) {
if (errors) {
res.json({
status: "error",
message: "please make sure your data is correct and your email and phone number are not valid"
});
return;
}
});
});
module.exports = router;
這將執行上body.email的所有falsy(https://developer.mozilla.org/de/docs/Glossary/Falsy)值的塊,不只是當它不存在。 – Florian
@Florian是的。你是對的。感謝您指出。我已更新。 – Sridhar
我認爲lodash在這裏是完全矯枉過正,爲什麼不(body.email === undefined)?查看isUndefined的來源:https://github.com/lodash/lodash/blob/4.17.4/lodash.js#L12212 – DevDig