问题
I am sending an id
from a jade view
to its controller
(backend), the controller then queries mongodb
and the result is sent to a new jade view
-angular
.
The results & view appear but URL
stays the same (previous view), thus if I F5
the current view, it reloads the previous view.
So the route is: view->node->angular
Ideas?
jade launcher view
form(method="post")
input(type="hidden" name="id" value=cart._id)
input(type='hidden', name='_csrf', value=_csrf)
button.btn.product.btn-primary(type='submit', id=cart._id class='')
controller
exports.pushCart = (req, res) => {
if (passport.isAuthenticated) {
Order.findOne({ _id: req.body.id})
.select('-_id productOrder')
.exec(function (err, orderObj) {
if(!err) {
var productOrder = orderObj.productOrder;
res.render('store/cart', {
title: 'MyCart',
angularApp: 'storeApp',
products: productOrder
});
}
});
} else {
res.redirect('/login');
}
};
Mongoose populate subdoc
回答1:
You need to redirect ,if you want the URL to change like this
res.redirect('/some/path')
回答2:
If you want to pass information while changing the URL you have to use session in order to transfer the data between pages.
connect-flash is very popular to use with passport for temporary session data transfer.
res.redirect('/store/card')
should be used in order to change the URL but it does not accept object to pass. That's the reason we use session in order to pass the information.
Here is your code should look like;
app.post('/yourPostURL', function(req, res){
// Auth and validate here, then
// Set a flash message by passing the key, followed by the value, to req.flash().
req.flash('success', 'Put anything here')
res.redirect('/store/card');
});
app.get('/store/card', function(req, res){
// Get an array of flash messages by passing the key to req.flash()
res.render('store/card', { messages: req.flash('success') });
});
来源:https://stackoverflow.com/questions/39620470/node-res-render-changes-view-but-not-url