html - 表单(方法 ='get' )适用于 Node.js,但状态 : pending in dev-tools

标签 html node.js pug

我正在制作一个网页,该网页使用表单将用户投票提交到使用 Node 的 mongo 数据库。虽然下面的代码成功修改了数据库,但它会导致 chrome 开发工具中出现挂起状态操作。这会阻止所有 js 运行,几分钟后浏览器会重定向到(不存在的)/v/foo。当使用“post”而不是“get”作为方法时也会出现此问题。

index.jade:

form(method= 'get', action= '/v/foo')
  button.vote(type= 'submit')

app.js: app.get('/v/:postID', home.vote);

home.js:

exports.vote = function(req, res){
  // gets postID from the URL
  postID = req.params.postID;
  // gets logged-in user
  user = req.session.username;
  // sends postID, retrieves a post with ID postID
  postdb.findPost(postID, function(post){
  // readPost(postID, function(post){
    // increments the 'votes' property of the comment by 1
    titles.update({postID: postID}, {$inc: {ups: 1}}, function(err){
      if (err) throw err;
    });

    // code that calculates current post level and progress goes here (works fine)

    // updates level and level progress
    titles.update({postID: postID}, {$set: {level: level, lvlProg: lvlProg}}, function(err){
      if (err) throw err;
    });

    accounts.update({username: user}, {$push: {votedPosts: postID}}, function(err){
      if (err) throw err;
      console.log('recorded')
    });

  });

最佳答案

您的代码不会发送响应,因此浏览器会感到困惑。 ;)

如果一切成功,请尝试 res.send(200);

HTTP 表单仍然期望其请求被彻底关闭,并且如果您不使用 res.send()end 它, Node 将愉快地无限期地保留该连接>.

关于html - 表单(方法 ='get' )适用于 Node.js,但状态 : pending in dev-tools,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15028865/

相关文章:

html - 使 <aside> 元素成为页面的特定部分?

php - 关于A4尺寸标记和打印问题

javascript - Express:为什么这个GET请求执行了两次?

pug - 在 Jade mixin 之间传递属性

javascript - 如果 var 存在,如何在查询中创建一个对象或丢弃该对象?

javascript - Pug/Jade 中动态包含的解决方法

css - 如何限制DIV在同一行

javascript - 使用 jQuery 或 Javascript 动态调整具有动态内容的 iFrame

node.js - 渲染附加到同一 block 的多个子模板

javascript - Push方法插入嵌套记录