我正在尝试验证表单,它工作正常,直到mysql发挥作用。当我验证validemail的数据时,它会向ejs页面呈现错误,如果字段为空,但是当我尝试将数据与我的数据库匹配时,数据不会被渲染。
//formvalidation.js
var body = '';
if(req.method === 'POST'){
// res.end('post');
console.log('Request found with POST method');
req.on('data', function (data) {
body += data;
console.log('got data:'+data);
});
req.on('end', function () {
var errorsFound=false;
var POST = qs.parse(body);
try
{
check(POST.email,'email').notEmpty().isEmail();
check(POST.pw,'pw').notEmpty();
}
catch(e)
{
errorsFound=true;
console.log("e value: "+e.message);
switch(e.message)
{
case 'email':
emsg[0]='Enter Valid Email';
break;
case 'pw':
psw[0]='pls Enter Password';
break;
}
}
if(errorsFound==false)
{
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'docs',
});
connection.connect(function(err) {
if (err) throw err;
});
var query=connection.query('SELECT * from user where username= '+connection.escape(POST.email)+' and password= '+connection.escape(POST.pw),function(err, rows) {
if(err) console.log(err);
console.log(rows.length);//it prints correct number of rows
console.log(query.sql);// it prints correct query that i intend to do
但在此之后没有任何作用,无论是否也是如此,我删除了if并给出了输入,以便至少查询得到一行然后它在else部分打印东西但是数组没有传递给form.ejs
if(rows.length)
{
res.end("You have successfully submitted a form");
}
else
{
values[0]=POST.email;
values[1]=POST.pw;
dberror[0]="Invalid username/password"
console.log(values);
console.log(dberror);
fs.readFile(__dirname + '/form.ejs','utf8', function (err, data) {
if (err) console.log(err);
//res.writeHead(200, {'Content-Type': 'text/html'});
var r=ejs.render(data,{
emailError:emsg,
pwError:psw,
values:values,
dberrors:dberror
});
res.write(r);
res.end();
});
}
});
connection.end();
}
else// this part works fine even both part contains exactly same code
{
console.log(emsg);
console.log(psw);
values[0]=POST.email;
values[1]=POST.pw;
console.log(values);
fs.readFile(__dirname + '/form.ejs','utf8', function (err, data) {
if (err) console.log(err);
res.writeHead(200, {'Content-Type': 'text/html'});
var r=ejs.render(data,{
emailError:emsg,
pwError:psw,
values:values,
dberrors:dberror
});
res.write(r);
res.end();
});
}
});
这是form.ejs
<body>
<div>
<form class="form-horizontal" method="post">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
<input type="text" name="email" id="inputEmail" placeholder="Email" <% if (values.length) { %>value='<%= values[0] %>'<% } %>>
</div>
</div>
<% if (emailError.length) { %><div class='error'><%= emailError[0] %></div><% } %>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" name="pw" id="inputPassword" placeholder="Password" <% if (values.length) { %>value='<%= values[1] %>'<% } %>>
</div>
</div>
<% if (pwError.length) { %>
<% pwError.forEach(function(name){ %>
<div class='error'><%= name %></div>
<% }) %>
<% } %>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn">Sign in</button>
<% if (dberrors.length) { %><div class='error'><%= dberrors[0] %></div><% } %>
</div>
</div>
</form>
</div>
</body>
您还需要了解更多信息吗?随便问一下