MongoLab上的Node.js和MongoDB:每个插入上的“套接字closures”

我试图做一些相对简单的事情,每当我尝试做一个“插入”,突然间就会遇到“服务器…- a.mongolab.com:36648套接字closures”错误。

读取似乎没有错误的工作,但插入似乎每次都会得到一个错误,我不知道它是我的代码(最近进行了一些小的改变),或者我在MongoLab使用的免费服务器的可靠性问题最近performance出下降几分钟)。

奇怪的是,logging本身似乎保存没问题,我只是把错误回来了!

任何人都可以看到我的代码问题,或者这可能是别的?

var mongoClient = require('mongodb').MongoClient; var http = require('http'); var connectionString = "..."; var pictureWallsCollectionName = 'PictureWalls'; //this is what barfs. see *** details exports.saveWall = function (req, res) { //reformat var toSave = { _id: req.body.wallId, pictures: req.body.pictures }; var status; mongoClient.connect(connectionString, function (err, db) { if (err) { return console.error(err); } var collection = db.collection(pictureWallsCollectionName); //*** no err yet... *** collection.insert( toSave, function (error, response) { //********************* //*** err here! ****** //********************* db.close(); if (error) { console.error(error); //bad status = 500; } else { console.log('Inserted into the ' + collection_name + ' collection'); //good status = 200; } }); response.status(status).end(http.STATUS_CODES[status]); }); } //this seems to work pretty reliably. including it just in case it's relevant exports.findByWallId = function (req, res) { var id = req.params.id; console.log('Retrieving wall: ' + id); mongoClient.connect(connectionString, function (err, db) { if (err) { return console.dir(err); } var collection = db.collection(pictureWallsCollectionName); collection.findOne( { _id: id }, function (err, item) { db.close(); if (err) { console.error(err); //something bad happened var status = 500; res.status(status).end(http.STATUS_CODES[status]); } else { console.log('Found wall with ID ' + id); //reformat and send back in the response res.send({ wallId: item._id, pictures: item.pictures }); } } ); }); }; 

编辑:我原来的问题的一部分是重复的参数名称。 查看链接的问题的细节。

原来的回应:这个问题结束了,我正在打电话:

  res.status(status).end(http.STATUS_CODES[status]); 

…asynchronous插入完成之前,所以它barfed。

但是,我并不确定如何在这种情况下发布响应。 在这里看到我的新问题:

如何在等待asynchronous方法完成时正确地发布响应?