gpt4 book ai didi

node.js - Node js Monk 更新持续存在

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:40 25 4
gpt4 key购买 nike

我是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 行,更新就永远不会结束,并且会持续到 future 。

有谁知道是什么原因造成的吗?

最佳答案

我已经解决了这个问题。我将其添加到此处,以防有人遇到同样的问题。

在代码中添加返回会停止该函数。

关于node.js - Node js Monk 更新持续存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33207288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com