node res.render changes view but not url

本秂侑毒 提交于 2021-01-28 18:59:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!