checkBody在提交时无法以表单的形式使用

问题描述 投票:0回答:1

当前,我正在尝试使用户注册到锦标赛帖子。预期的输出是注册到锦标赛帖子的用户将被保存到模型收藏名称寄存器中。但是,如果在单击“提交”按钮后,我将“全名”和“联系人”两个字段都留了下来,则错误仍然存​​在cannot read property id of undefined。否则,如果同时输入了两个字段,则提交。我在这里想念什么吗?

tournament.pug(将用户重定向到注册表的按钮)

a.btn.btn-outline-dark.btn-sm(href='/register/form/'+tournament._id) Join

register_tournament.pug(用户提交前检查确认详细信息的表格,还需要输入全名和联系方式,因为其他字段的值来自当前用户和锦标赛)

form(method='POST', action='/register/submit')
        #form-group
            strong Tournament Id
            input.form-control(type='hidden', name='tournamentId' value=tournament.id)
            p.text-muted your tournament id
        #form-group
            strong Tournament Name
            input.form-control(type='text', name='tournamentName', value=tournament.title, readonly)
            p.text-muted your tournament name
        #form-group
            strong User Id
            input.form-control(type='hidden', name='playerId' value=user.id)
            p.text-muted your user id
        #form-group
            strong Username
            input.form-control(type='text', name='playerUsername', value=user.username, readonly)
            p.text-muted your steam username
        #form-group
            strong Steam Id
            input.form-control(type='hidden', name='playerSteamId', value=user.steamId)
            p.text-muted your steam id
        #form-group
            strong Full Name
            input.form-control(type='text', name='playerName')
            p.text-muted your full name
        #form-group
            strong Contact Number
            input.form-control(type='text', name='playerContact')
            p.text-muted your contact number
        input.btn.btn-outline-dark.btn-sm(type='submit', value='Submit')

register.js(routes / register.js)

//Render Register Tournament
router.get('/form/:id', ensureAuthenticated, function(req, res){
    Tournament.findById(req.params.id, function(err, tournament){
        User.findById({user: req.user_id}, function(err, user){
            res.render('register_tournament',{
                user: req.user,
                tournament: tournament
            });
        });
    });
});

//Submit Register Tournament
router.post('/submit', ensureAuthenticated, function(req, res){

            req.checkBody('playerName', 'Full name is required').notEmpty();
            req.checkBody('playerContact', 'Contact number is required').notEmpty();

            let errors = req.validationErrors();

            if(errors){
                res.render('register_tournament', {
                    errors: errors,
                    user: req.user,
                });
            }
            else{
                let register = new Register();
                register.tournamentId = req.body.tournamentId;
                register.tournamentName = req.body.tournamentName;
                register.playerId = req.body.playerId;
                register.playerUsername = req.body.playerUsername;
                register.playerSteamId = req.body.playerSteamId;
                register.playerName = req.body.playerName;
                register.playerContact = req.body.playerContact;

                register.save(function(err){
                    if(err){
                        console.log(err);
                        return;
                    }
                    else{
                        req.flash('success', 'User registered');
                        res.redirect('/');
                    }
                });    
            }
});

UPDATE:如果我在两个字段中输入全名和联系电话,则注册有效。但是,如果我将其保留为空白,则我的验证不适用于两个字段。有什么想法吗?

node.js mongodb express pug
1个回答
0
投票

已修复提交,这是我如何解决的问题。

router.post('/submit', ensureAuthenticated, function(req, res){

    Tournament.findOne({tournament:req.tournament}, function(err,tournament){
        req.checkBody('playerName', 'Full name is required').notEmpty();
        req.checkBody('playerContact', 'Contact number is required').notEmpty();
        req.checkBody('tournamentId', 'Tournament id is required').notEmpty();
        req.checkBody('tournamentName', 'Tournament name is required').notEmpty();
        req.checkBody('playerId', 'Player id is required').notEmpty();
        req.checkBody('playerUsername', 'Player username is required').notEmpty();
        req.checkBody('playerSteamId', 'Player steam id is required').notEmpty();

        let errors = req.validationErrors();

        if(errors){
            res.render('register_tournament', {
                errors: errors,
                user: req.user,
                tournament: tournament
            });
        }
        else{
            let register = new Register();
            register.tournamentId = req.body.tournamentId;
            register.tournamentName = req.body.tournamentName;
            register.playerId = req.body.playerId;
            register.playerUsername = req.body.playerUsername;
            register.playerSteamId = req.body.playerSteamId;
            register.playerName = req.body.playerName;
            register.playerContact = req.body.playerContact;

            register.save(function(err){
                if(err){
                    console.log(err);
                    return;
                }
                else{
                    req.flash('success', 'User registered');
                    res.redirect('/');
                }
            });    
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.