
Question:
when i run this code i get redirect to postUser page when i want to redirect if condition is true and render view if condition is false...
<pre class="lang-js prettyprint-override">router.get('/postedUser/:id', ensureAuthenticated, function(req, res) {
User.findOne({_id: req.params.id}, function(err, data) {
if (err) throw err;
if (req.user._id == data._id) {
res.redirect('/users/profile');
} else {
res.render('postedUser', {
data: data
});
}
});
});
can some one tell me where am going wrong..
Answer1:You can not do so if you are submitting request using an API call from Ajax or other methods.
As all these methods are to implement <strong>Single Page Application</strong>, so rendering a different page is not allowed.
What happens when condition is met and request to render a different page?
The module who called API is waiting for response on browser to get some response from API, however your API is trying to load a different HTML page which browser prohibits and thus you will never get rendered via Ajax call.
How to do it?
There are two way out:
<ol><li>Single Page Application: Using JavaScript Frameworks like AngularJs, ReactJs will help you out by just changing HTML templates and not whole page.</li> </ol>With Ajax you can do this:
HTML
<body>
<div>
<div align="center">
<form id="form1">
<label>Please enter below details</label><br><br>
<label>First Name *: </label><input id="FName" type="text" name="FName"/><br><br>
<label>Last Name *: </label><input id="LName" type="text" name="LName"/><br><br>
<label>Email Address *: </label><input type="email" name="email"><br><br>
<br><br>
<input type="button" value="Submit" onClick:"submitForm()"/>
</form>
<div id="showValue"></div>
</div>
</div>
</body>
JavaScript
function submitForm() {
$.ajax({
url: '/addName',
type: 'POST',
headers: headers,
data: {
"Fname": $("#FName").val(),
"Lname": $("#LName").val(),
"email": $("#email").val()
},
success: function(result) {
//append result to your html
//Dynamic view
$("#form1").hide();
var html = '<div><span>First Name: ' + result.fName + '</span><span>Last Name: ' + result.lName + '</span></div>';
$("#showValue").html(html);
},
error: function (error) {
alert('error ',error);
}
});
}
Server side code I'm assuming you are using express.js and body-parser
app.post('/addName', (req, res) => {
//Insert into db
var body = req.body;
res.send({
fName: body.FName,
lName: body.LName
});
});
<ol start="2"><li>Use form
Submit method: using this you will be able to render the page however you can't send response back to same page i.e.,</li>
</ol>HTML
<body>
<div>
<div align="center">
//Can't send params using this approach
//Data will be send as query string
<form action="/postedUser/" method="GET">
<label>Please enter below details</label><br><br>
<label>ID *: </label><input id="ID" type="text" name="ID"/><br><br>
<input type="submit" value="Submit" /></form>
</div>
</div>
</body>
NodeJS
//If there will be res.send({});
// This won't be returned to same page. But your code doesn't do any such thing
router.get('/postedUser/', ensureAuthenticated, function(req, res) {
User.findOne({_id: req.query.id}, function(err, data) {
if (err) throw err;
if (req.user._id == data._id) {
res.redirect('/users/profile');
} else {
res.render('postedUser', {
data: data
});
}
});
});