我在nodejs中有一个注册表。
以这种形式,我必须检查phoneNumber是否存在于数据库中。
我使用passportJs
进行此操作。
但是当我发送请求时,会显示此错误:
无法读取未定义的属性“ body”
现在我在这里写我的代码:
这是在中间件中设置的护照:
require('./passport/passport-local');
app.use(passport.initialize());
app.use(passport.session())
这是我项目中的passport-local
文件:
const passport = require('passport');
const localStrategy = require('passport-local').Strategy;
const UserModel = require('../models/users')
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
passport.use('local.register', new localStrategy({
phoneNumberField: 'phoneNumber',
passReqToCallback: true,
}, (req, phoneNumber, done) => {
UserModel.findOne({ phoneNumber: phoneNumber }, (error, user) => {
if (error) return done(error);
if (!user) return done(error, false, req.flash('errors', 'This Phone Number exist . Please Choose the Other Phone Number'))
const addUSer = new UserModel({
name: req.body.name,
family: req.body.family,
phoneNumber: req.body.phoneNumber,
password: req.body.password
})
addUSer.save((err) => {
if (err) return done(err, false, req.flash('errors', 'امکان ثبت کاربر با این مشخصات وجود ندارد'));
done(null, addUSer);
});
})
}))
这是我的控制器:
RegisterProcess(req, res) {
var validateResult = validationResult(req);
if (!validateResult.isEmpty()) {
const errors = validateResult.array();
let messages = [];
errors.forEach(error => {
messages.push(error.msg);
});
req.flash('errors', messages)
res.redirect('/auth/register');
}else{
this.register();
}
}
register(req, res, next) {
passport.authenticate('local.register', {
successRedirect: '/admin',
failureRedirect: '/auth/register',
failureMessage: true
})(req, res, next)
}
现在是什么问题?我该如何解决这个问题?
**编辑:**
全栈跟踪:
TypeError: Cannot read property 'body' of undefined
at Strategy.authenticate (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/passport-local/lib/strategy.js:71:29)
at attempt (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/passport/lib/middleware/authenticate.js:366:16)
at authenticate (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/passport/lib/middleware/authenticate.js:367:7)
at RegisterController.register (/home/mrcoder/Projects/Nodejs/OnlineStore/app/http/controller/auth/RegisterController.js:36:11)
at RegisterController.RegisterProcess (/home/mrcoder/Projects/Nodejs/OnlineStore/app/http/controller/auth/RegisterController.js:27:18)
at Layer.handle [as handle_request] (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/express/lib/router/layer.js:95:5)
at next (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/express/lib/router/route.js:137:13)
at middleware (/home/mrcoder/Projects/Nodejs/OnlineStore/node_modules/express-validator/src/middlewares/check.js:15:13)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
答案 0 :(得分:0)
为了能够从提交的html表单中解析数据,您需要一个适当的parsing-middleware。一种常见的库是body-parser。您可以按以下步骤进行设置:
const bodyParser = require('body-parser');
...
// use bodyParser middleware for parsing application/xwww-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));