MySQL INSERT与窗体后页面不呈现

时间:2018-04-28 14:50:04

标签: html mysql node.js handlebars.js

我想在成功提交添加用户表单后让我的管理仪表板页面呈现。插入用户时正在执行以下代码:

app.get('/insert-user',function(req,res,next){
  var context = {};
  pool.query("INSERT INTO user (`fname`, `lname`, `email`, `password`,`timestamp`,`company`) VALUES (?,?,?,?,NOW(),?)", 
  [req.query.fname, req.query.lname, req.query.email, req.query.password, req.query.company], 
  function(err, result){
    if(err){
      console.log(err);
      next(err);
      return;
    }
    res.render('adminDash',context);
  });
});

记录已成功插入表中,但页面仍保留在已填充的表单上,并且不会呈现adminDashadminDash把手包含以下内容:

<h1>Admin Dashboard</h1>

<p><a href="http://18.219.103.143:3000/add-user">Add a User</a></p>
<p><a href="http://18.219.103.143:3000/edit-delete-user">Edit/Delete a User</a></p>
<p><a href="http://18.219.103.143:3000/add-admin-user">Add Admin User</a></p>
<p><a href="http://18.219.103.143:3000/edit-delete-admin-user">Edit/Delete Admin User</a></p>
<p><a href="http://18.219.103.143:3000/admin-login">Generate Report</a></p>

addUser表单包含以下内容:

<form id="userForm">
        <fieldset>
            <legend>Add User</legend>
            <p>First Name: <input id="fname" type="text" name="fname"/></p>
            <p>Last Name: <input  id="lname" type="text" name="lname"/></p>
            <p>Email: <input id="email" type="text" name="email"/></p>
            <p>Company: <input id="company" type="text" name="company"/></p>
            <p>Password: <input id="password" type="text" name="password"/></p>
            <button type="button">Attach Signature</button>
        </fieldset>
        <input id="addUser" type="submit" name="add" value="Add User" onclick="addRow()" />


</form>

<script src="script.js"></script>

我是否在错误的位置渲染了渲染?它试图呈现的页面有问题吗?谢谢!

2 个答案:

答案 0 :(得分:0)

尝试这样做

models.User.create(function({
         //Your Query goes here
})
.then(function(result){
  res.render('',{
   result:result

});

}).catch(function(err){
 console.log(err);
})

and in your ADmin Dashboard try to call this Result in a forEach loop

<% result.forEach(results) %>

答案 1 :(得分:0)

{
                models.Page.findOne({
                    where :{slug:slug}
                    })
                            models.Page.create({
                                title: title,
                                slug: slug,
                                content: content,
                                sorting: 0
                            })

                                .then((page)=>{

                                req.flash('success', 'Page added!');
                                res.redirect('/admin/pages');
                            })
                            .catch((err) => {
                            console.log(err);
                            })
                        }
                    });