my code is:
<%
var result = [];
if(result.length>0)
{
//do nothing
}
else
{
//redirect page to "/product-not-found"
//window.location.replace(""); is not working
}
%>
Note: without passing res, req from node.js controller
when i add this code:
window.location.replace("");
it's not working for me. show me error:
window is not defined
at eval (eval at pile (\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:215:37)
at returnedFn (\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:482:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\node_modules\express\lib\response.js:1012:7)
at \controllers\productController.js:1274:8
at \node_modules\mongoose\lib\model.js:4733:16
at \node_modules\mongoose\lib\utils.js:263:16
at \node_modules\mongoose\lib\aggregate.js:973:13
at \node_modules\kareem\index.js:135:16
at processTicksAndRejections (internal/process/task_queues.js:82:9)
my code is:
<%
var result = [];
if(result.length>0)
{
//do nothing
}
else
{
//redirect page to "/product-not-found"
//window.location.replace("http://stackoverflow."); is not working
}
%>
Note: without passing res, req from node.js controller
when i add this code:
window.location.replace("http://stackoverflow.");
it's not working for me. show me error:
window is not defined
at eval (eval at pile (\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:215:37)
at returnedFn (\node_modules\ejs\lib\ejs.js:653:17)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:251:36)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:482:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\node_modules\express\lib\response.js:1012:7)
at \controllers\productController.js:1274:8
at \node_modules\mongoose\lib\model.js:4733:16
at \node_modules\mongoose\lib\utils.js:263:16
at \node_modules\mongoose\lib\aggregate.js:973:13
at \node_modules\kareem\index.js:135:16
at processTicksAndRejections (internal/process/task_queues.js:82:9)
Share
Improve this question
edited Jun 21, 2019 at 11:07
Shubham Dixit
1
asked Jun 21, 2019 at 7:22
Nirav SutariyaNirav Sutariya
3071 gold badge4 silver badges14 bronze badges
1
- 1 it's not working @SiddharthDas i have already try it it's not js it's "product.ejs" page – Nirav Sutariya Commented Jun 21, 2019 at 7:31
3 Answers
Reset to default 3Here is a hack you can try
1-Create a file called redirect.ejs with following code
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="refresh" content="1;url=<%= url %>">
<script type="text/javascript">
window.location.href = "google."
</script>
</head>
<body>
If you are not redirected automatically, <a href="<%= url %>">click here</a>.
</body>
</html>
2-Call this file as partial in your code like this
<%
var result = [];
if(result.length>0)
{
//do nothing
}
else
{
%>
<%- include('redirect.ejs')%>// redirect to
<%
}%>
Let me know if it works for you
Until now, I didn't find a way to redirect from ejs page to another page, IF you are using data from the variabile that you should use in that ejs page.
So my solution is to redirect from the server-side, instead from the client-side. In the example below, there is a login page which, at successful login, renders the account page, and, at unsuccessful login, renders the login page again.
File: node.js
//Profile page
app.get('/account', function (req, res) {
if(req.session.user){
// user can access account page
let sess = req.session;
console.log(sess);
res.render('account', {user: sess.user});
}
else{
// user cannot access account page
// -> display again the login page
res.render('login');
}
});
I may be late to the party but I had this issue and this is how I solved it:
<% if (yourBoolean) { %>
<script> document.location.href = '/your-redirect' </script>
<% } %>
Forcing a redirect via a script tag, with EJS, not a redirect from EJS.
Happy coding!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744368541a4570827.html
评论列表(0条)