开发者

nodejs mongoose database access randomly doesnt' return a result

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.pars开发者_StackOverflow中文版e(_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?


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);

        }
});
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜