我正在制作一个网页,该网页使用表单将用户投票提交到使用 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/