Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在nodejs Express上生成错误时如何保留表单数据_Node.js_Express_Handlebars.js - Fatal编程技术网

Node.js 在nodejs Express上生成错误时如何保留表单数据

Node.js 在nodejs Express上生成错误时如何保留表单数据,node.js,express,handlebars.js,Node.js,Express,Handlebars.js,发生错误时如何保留表单数据。通常在表单提交时重新加载页面。我使用的是无手柄、快速验证器、连接闪存。 这是我的注册代码: //Register User router.post('/register', function(req, res){ var name = req.body.name; var email = req.body.email; var username = req.body.username; var password = req.body.p

发生错误时如何保留表单数据。通常在表单提交时重新加载页面。我使用的是无手柄、快速验证器、连接闪存。 这是我的注册代码:

//Register User
router.post('/register', function(req, res){
    var name = req.body.name;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;


    //Validation
    req.checkBody('name', 'Name is required!').notEmpty();
    req.checkBody('email', 'Email is required!').notEmpty();
    req.checkBody('email', 'Email is not valid!').isEmail();
    req.checkBody('username', 'Username is required!').notEmpty();
    req.checkBody('password', 'Password is required!').notEmpty();
    req.checkBody('password', 'Minimum Length of Password is 1!').isLength({min: 1});
    req.checkBody('password2', 'Password do not match!').equals(req.body.password);


    var errors = req.validationErrors();
    if(errors){
//want some help here by which if any error generate then all value is automatically filled. So that everytime there is no need to fill full form
        res.render('register', {
            errors: errors
        });
    }else{
//Here Success Part Code runs

    }
});
此代码是视图部分(寄存器.把手) //任何帮助都将不胜感激。 此页是关于把手的。如果表单生成错误,则将旧数据显示到表单中会有点混淆 登记册

    {{#if errors}}
        {{#each errors}}
            <div class="alert alert-danger">{{msg}}</div>
        {{/each}}
    {{/if}}
    <form method="post" action="/custom/register">
        <div class="form-group">
                    <label>Name</label>
                    <input type="text" name="name" class="form-control" placeholder="Name">
                </div>
                <div class="form-group">
                    <label>Username</label>
                    <input type="text" name="username" class="form-control" placeholder="Userame">
                </div>
                <div class="form-group">
                    <label>Email</label>
                    <input type="email" name="email" class="form-control" placeholder="Password">
                </div>
                <div class="form-group">
                    <label>Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Password">
                </div>
                <div class="form-group">
                    <label>Confirm-Password</label>
                    <input type="password" name="password2" class="form-control" placeholder="Password">
                </div>

                <button type="submit" class="btn btn-success">Submit</button>
    </form>
{{{#if errors}
{{{#每个错误}
{{msg}}
{{/每个}}
{{/if}
名称
用户名
电子邮件
密码
确认密码
提交
//注册用户
路由器post(“/寄存器”,函数(req,res){
var name=req.body.name;
var email=req.body.email;
var username=req.body.username;
var password=req.body.password;
var password2=req.body.password2;
//字段值持有者
变量形式={
姓名持有者:req.body.name,
用户名持有者:req.body.username,
emailholder:req.body.email
};
//验证
req.checkBody('name','name是必需的!')。notEmpty();
req.checkBody('email','email是必需的!')。notEmpty();
req.checkBody('email','email无效!').isEmail();
req.checkBody('username','username是必需的!')。notEmpty();
req.checkBody('password','password是必需的!')。notEmpty();
req.checkBody('password','password的最小长度为1!')。isLength({min:1});
req.checkBody('password2','Password不匹配!')。等于(req.body.Password);
var errors=req.validationErrors();
如果(错误){
res.render('寄存器'{
错误:错误,
表格:表格
});
}否则{
//这里运行成功部分代码
}
});
//登记。车把页
//只需增加持有人的价值
登记
{{#如果有错误}
{{{#每个错误}
{{msg}}
{{/每个}}
{{/if}
名称
用户名
电子邮件
密码
确认密码
提交

您必须将表单数据发送回客户端,以便在用户浏览器中使用

res.render('register', {
  errors: errors,
  formData: {
    name: name,
    email: email,
    username: username
  }
});
然后,修改车把模板,使其使用这些值:

<form method="post" action="/xad/register">
    <div class="form-group">
        <label>Name</label>
        <input type="text" name="name" class="form-control" placeholder="Name" value="{{ name }}">
    </div>

    <div class="form-group">
        <label>Username</label>
        <input type="text" name="username" class="form-control" placeholder="Userame" value="{{ username }}">
    </div>

    <div class="form-group">
        <label>Email</label>
        <input type="email" name="email" class="form-control" placeholder="Password">
    </div>

    <div class="form-group">
        <label>Password</label>
        <input type="password" name="password" class="form-control" placeholder="Password">
    </div>

    <div class="form-group">
        <label>Confirm-Password</label>
        <input type="password" name="password2" class="form-control" placeholder="Password">
    </div>

    <button type="submit" class="btn btn-success">Submit</button>
</form>

名称
用户名
电子邮件
密码
确认密码
提交

您的意思是,服务器从视图中获取所有数据,而不管其中是否存在错误。在服务器端文件中检查数据是否有效。然后将错误打印到视图中。您希望在下次提交表单时保留正确的数据。是吗?我只是希望当出现验证错误时,整个表单文件都必须填写。因此,除了填充所有字段之外,还有什么解决方案可以让所有其他字段保持不变,或者注入值,以便只填充错误字段值,而不是全部。您可以使用json对象存储所有有效的表单字段并返回它。因此,您可以在视图部分捕获它,并使用html logicstore从req.body存储json对象。假设您创建了一个名为obj_not_error的。在渲染方法期间传递此对象以及错误,并在视图中发布这些json对象。res.render('寄存器',{errors:errors,obj_not_error:obj_not_error});谢谢你的回复,但我想我也完成了同样的任务。无论如何,谢谢你的支持。我想再问一件事,我正在使用连接闪存。所以req.flash('error\u msg','your hand Message')在res.redirect()之前可以正常工作,但同样的事情在res.render上不起作用。我的代码是res.render('register',{errors:req.flash('error\u msg'),form:form});您可能应该提出另一个问题,以便了解Connect flash的人能够帮助您解决问题。无论如何,也许这可以帮助你:
<form method="post" action="/xad/register">
    <div class="form-group">
        <label>Name</label>
        <input type="text" name="name" class="form-control" placeholder="Name" value="{{ name }}">
    </div>

    <div class="form-group">
        <label>Username</label>
        <input type="text" name="username" class="form-control" placeholder="Userame" value="{{ username }}">
    </div>

    <div class="form-group">
        <label>Email</label>
        <input type="email" name="email" class="form-control" placeholder="Password">
    </div>

    <div class="form-group">
        <label>Password</label>
        <input type="password" name="password" class="form-control" placeholder="Password">
    </div>

    <div class="form-group">
        <label>Confirm-Password</label>
        <input type="password" name="password2" class="form-control" placeholder="Password">
    </div>

    <button type="submit" class="btn btn-success">Submit</button>
</form>