我试图在EJS模板中读取URL参数,但是我尝试或从google读取的所有内容都不起作用。下面是我的app.js文件的相关部分:
app.post('/account/logintoaccount', function(req, res) {
var email = req.body.email;
if(email === '') {
res.redirect('https://myurl.com/account/login?e=bademail');
return;
}
var password = req.body.password;
if(password === '') {
res.redirect('https://myurl.com/account/login?e=badpassword');
return;
}
res.redirect('https://myurl.com/?email=' + email + '&pw=' + password);
});下面是我试图在EJS文件中显示变量的地方:
<div id="title-text">
<% if(!req.query.e) { %>
<p>Log into your account:</p>
<% } else if(req.query.e === "badpass") { %>
<div class="alert alert-danger"><strong>Error</strong> incorrect password</div>
<% } else if(req.query.e === "bademail") { %>
<div class="alert alert-danger"><strong>Error</strong> unknown email address</div>
<% } %>
</div>如果删除EJS文件的这一部分,则重定向工作很好。URL中有参数,一切都很好。然而,每当我尝试以任何方式在EJS文件中使用我的URL参数时,它都会中断并给我一个"X是未定义的错误“。我试过使用“e”、“req.query.e”、“that .e”、“that.req.query.e”以及google结果建议我使用的其他任何东西。
我的最终目标是能够读取https://myurl.com/account/login?e=somethinghere中的URL参数
那就检查一下什么东西。我花了最后的30+分钟阅读谷歌的结果,但我认为它们要么过时了,要么我只是做错了。有人能帮忙吗?谢谢您抽时间见我
发布于 2017-03-20 01:41:55
将请求传递到视图中即可。
app.get('/account/:action', function(req, res) {
res.render('account/' + req.params.action, {
session: req.session,
req: req
});
});https://stackoverflow.com/questions/42894149
复制相似问题