我刚接触node.js,mongoDB,monk和express (都是最新版本)。当我工作的时候,我遇到了下一种情况:
我有一个更新的表,并且我定期从它读取。当我从其中读取数据时,我会更新数据库中的一个标志,以防止重新读取。(我不想删除它,以防我需要检查一些东西)。
现在的问题是,在我使用monk中的更新后,它将永远存在。每次我添加的帖子被标记后,我必须重新启动服务器才能让它恢复正常。当我阅读的时候,这个问题又出现了。
我设法通过在代码中添加req.pause()来解决这个问题--但我对此感到不舒服,因为我不想让一堆活动请求阻塞服务器。
下面是我的代码:
function(req,res){
this.getNotifications = function(req,res){
var username = req.params.username;
var justOpened = req.params.justOpened;
var callback = function(e,doc) {
if(doc != ""){
db.update(username + "Notifications",{read:0},{$set:{read:1}},function(){});
req.pause(); // if I delete this the update keeps going
res.json(doc);
res.status = 200;
res.end();
} else {
setTimeout(function(){db.find(username + "Notifications",read:0},{},callback);},5000);
}
};
if (justOpened == "true"){
db.find(username + "Notifications",{},{},callback);
} else {
db.find(username + "Notifications",{read : 0},{},callback);
}
}我的database.update方法看起来像这样:
this.update = function(collectionName,queryObj,update,callback){
var collection = db.get(collectionName);
collection.update(queryObj,update,{multi:true},callback);
}基本上,如果我删除了req.pause行,更新就永远不会结束,而且会一直持续到将来。
有人知道这些是什么原因吗?
发布于 2015-10-23 14:00:48
我已经解决了这个问题。我在这里添加它,以防有人遇到同样的问题。
向代码添加return将停止该函数。
https://stackoverflow.com/questions/33207288
复制相似问题