nodejs mongoose数据库随机访问不返回结果

发布于 2024-11-15 07:29:35 字数 1999 浏览 3 评论 0原文

我正在开发我的第一个 NodeJS 应用程序,它基本上只是使用 MongoDB(托管在 MongoHQ 上)数据库的宁静 Web 服务。

我正在使用 Express 和 Mongoose 来协助路由和数据库访问,并且它似乎在大多数情况下都运行良好,但是随机地我会遇到一个问题,即我编写的​​用于访问数据库的中间件方法之一启动,并调用 mongoose 查找,但它永远不会得到响应,最终 http 请求超时。

背景知识,我正在处理的域对象被称为“convoy”,它有一个 convoyCode 这是它的标识符,我试图根据这个来查找它。

这是被调用的 POST 方法的方法

function findConvoy(request, response, next){
    var convoyCode;

    if (request.method == 'GET'){
        var _url = url.parse(request.url);
        var queryString = qs.parse(_url.query); 
        convoyCode = queryString.ConvoyCode;
    } else {
        convoyCode = request.body.ConvoyCode;
    }

    console.log("Finding Convoy " +  convoyCode);

    Model.findOne({ name:convoyCode}, function(err, convoy){
            if (err){
                console.log("Error finding convoy " + err);
                                    response.send('ERROR FINDING CONVOY', 500);

            }
            if (convoy){
                request.ReturnedConvoy = convoy;
                    console.log("Found Convoy " + convoyCode);
                next();
            } else {
                console.log("Convoy not found.");
                response.send('CONVOY NOT FOUND', 404);
            }
    });
}

如下:

    app.post("/JoinConvoy",  findConvoy, function(request, response){
    //stuff here

    }

我的数据库模型

    var Convoy = new mongoose.Schema({

      name: String
    , creator: String
    , startDate: Date
    , destinationLat: Number
    , destinationLong: Number
    , currentLocations: [Locations]

});

//get the model
var Model = mongoose.model('Convoy', Convoy);

不是那么重要。大多数时候一切都工作正常,但似乎当我让服务器运行一段时间而不使用它时,我回来时它就无法连接,直到我重新启动它。

以下是控制台日志:

ID 57C18326-7EBB-5A99-88BD-D34E9F108D98 尝试更新 Convoy ZX12 用户 B 在 ZX12 上更新了位置 寻找车队ZX12 查找 Convoy ZX12

您可以看到最后 2 次查找 Convoy ZX12 是我尝试调用该方法的两次,之前的所有内容都是几小时前发送的最后一个请求的日志。

无论我尝试重新连接多少次,它都不起作用。有人知道它可能是什么吗?

I'm working on my first NodeJS application, which is basically just restful web services that use a MongoDB (hosted on MongoHQ) database.

I'm using Express and Mongoose to assist with routing and database access, and it seems to work well most of the time, however randomly I'll get an issue where one of my middleware methods that I've written to access the database starts, and calls the mongoose lookup but it never gets a response and eventually the http request times out.

Background knowledge, my domain object I'm dealing with is called a 'convoy' it has a convoyCode which is its identifier, I'm trying to look it up based on this.

Here is the method

function findConvoy(request, response, next){
    var convoyCode;

    if (request.method == 'GET'){
        var _url = url.parse(request.url);
        var queryString = qs.parse(_url.query); 
        convoyCode = queryString.ConvoyCode;
    } else {
        convoyCode = request.body.ConvoyCode;
    }

    console.log("Finding Convoy " +  convoyCode);

    Model.findOne({ name:convoyCode}, function(err, convoy){
            if (err){
                console.log("Error finding convoy " + err);
                                    response.send('ERROR FINDING CONVOY', 500);

            }
            if (convoy){
                request.ReturnedConvoy = convoy;
                    console.log("Found Convoy " + convoyCode);
                next();
            } else {
                console.log("Convoy not found.");
                response.send('CONVOY NOT FOUND', 404);
            }
    });
}

the POST method being called looks like:

    app.post("/JoinConvoy",  findConvoy, function(request, response){
    //stuff here

    }

my database model is

    var Convoy = new mongoose.Schema({

      name: String
    , creator: String
    , startDate: Date
    , destinationLat: Number
    , destinationLong: Number
    , currentLocations: [Locations]

});

//get the model
var Model = mongoose.model('Convoy', Convoy);

Not that it should matter that much. Everything works perfectly most of the time, but it seems to be when I leave my server running for a bit without using it I come back and it just won't connect until I reboot it.

Here's the console log:

ID 57C18326-7EBB-5A99-88BD-D34E9F108D98 attempting to update Convoy ZX12
User B updated their location on ZX12
Finding Convoy ZX12
Finding Convoy ZX12

You can see the last 2 finding Convoy ZX12's are two times I've tried to call the method, everything before that is the logs from the last request sent hours ago.

No matter how many times I try to reconnect it doesn't work. Has anyone got any ideas as to what it could be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-11-22 07:29:35

尝试使用 end 而不是 send 方法

function findConvoy(request, response, next){
  var convoyCode;

  if (request.method == 'GET'){
    var _url = url.parse(request.url);
    var queryString = qs.parse(_url.query); 
    convoyCode = queryString.ConvoyCode;
  } else {
    convoyCode = request.body.ConvoyCode;


  console.log("Finding Convoy " +  convoyCode);

  Model.findOne({ name:convoyCode}, function(err, convoy){
        if (err){
            console.log("Error finding convoy " + err);
                                response.send('ERROR FINDING CONVOY', 500);

        }
        if (convoy){
            request.ReturnedConvoy = convoy;
                console.log("Found Convoy " + convoyCode);
            next();
        } else {
            console.log("Convoy not found.");
            // end instead on send    
            response.end('CONVOY NOT FOUND', 404);

        }
});
}

Try putting end instead of send method

function findConvoy(request, response, next){
  var convoyCode;

  if (request.method == 'GET'){
    var _url = url.parse(request.url);
    var queryString = qs.parse(_url.query); 
    convoyCode = queryString.ConvoyCode;
  } else {
    convoyCode = request.body.ConvoyCode;


  console.log("Finding Convoy " +  convoyCode);

  Model.findOne({ name:convoyCode}, function(err, convoy){
        if (err){
            console.log("Error finding convoy " + err);
                                response.send('ERROR FINDING CONVOY', 500);

        }
        if (convoy){
            request.ReturnedConvoy = convoy;
                console.log("Found Convoy " + convoyCode);
            next();
        } else {
            console.log("Convoy not found.");
            // end instead on send    
            response.end('CONVOY NOT FOUND', 404);

        }
});
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文