【问题标题】:location.reload re-send postlocation.reload 重新发送帖子
【发布时间】:2022-01-14 04:17:08
【问题描述】:

我每 5 秒执行一次超时以运行 location.reload(),但是在页面上我有一些表单可以向 mongoDB 发送一些帖子,一旦我这样做,它会将我重定向回页面以使更新可见。问题是一旦重定向location.reload() 将重新发送帖子并在 MongoDB 中创建新记录。有没有办法避免这种情况?

这是我的代码示例:

orders.js

...
if(ActionDOM == 'accept'){
        FinalOrder.findOneAndUpdate({'_id':orderId},{
            "$set": {
                'orderStat':1,
                'last_update':Date.now()
            }
        }).then(order => {
            UpdateItem.aggregate([
                {
                    $group:{
                        _id:'a',
                        minNum:{
                            $max:'$idUpdate'
                        }
                    }
                }
            ]).then(u =>{
                var idUpdate = (u.length==0)?1:u[0].minNum+1
                const update = new UpdateItem({
                    idUpdate,
                    idUser:req.user._id,
                    idOrder:orderId,
                    orderStat:1
                })
                update.save().then(o =>{
                    Promise.all([mongoQueries.activeOrders(req.user._id.toString()),mongoQueries.menuItems(),mongoQueries.openOrders()]).then(info => {
                        res.render('openOrders', {pageTitle:'My orders', navBarOn:'1', user:req.user, info:info, refresh:1})
                    }).catch(e => console.log(e))
                }).catch(e => console.log(e))
            }).catch(e => console.log(e))
        }).catch(e => console.log(e))
    }

orders.ejs

...
<% if(locals.refresh){ %>
  <% if(locals.refresh==1){ %>
      <script>
        setTimeout(location.reload(),5000)
      </script>
  <% } %>
<% } %>
...

【问题讨论】:

    标签: javascript node.js mongodb ejs


    【解决方案1】:
    location.reload() // it reloads page with post data.
    

    试试

    window.location = ""; // opens a page without post data
    

    window.location = document.URL; // opens a current page without post data
    
    <% if(locals.refresh){ %>
      <% if(locals.refresh==1){ %>
          <script>
            setTimeout(function() {
              window.location = "";
            },5000)
          </script>
      <% } %>
    <% } %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多