MongoLab上的Node.js和MongoDB:“;“插座关闭”;每次插入

MongoLab上的Node.js和MongoDB:“;“插座关闭”;每次插入,node.js,mongodb,sockets,azure,mlab,Node.js,Mongodb,Sockets,Azure,Mlab,我试图做一些相对简单的事情,每次我尝试“插入”时都会突然遇到“服务器…-a.mongolab.com:36648套接字关闭”错误 读取似乎可以正常工作,但插入似乎每次都会出错,我不确定这是我的代码(最近做了一些小改动)还是我在MongoLab使用的免费服务器的可靠性问题(最近它显示自己停机了几分钟) 奇怪的是,记录本身似乎保存得很好,我只是把错误拿回来 有人能看到我的代码有问题吗,或者这是其他问题吗 var mongoClient = require('mongodb').MongoClient

我试图做一些相对简单的事情,每次我尝试“插入”时都会突然遇到“服务器…-a.mongolab.com:36648套接字关闭”错误

读取似乎可以正常工作,但插入似乎每次都会出错,我不确定这是我的代码(最近做了一些小改动)还是我在MongoLab使用的免费服务器的可靠性问题(最近它显示自己停机了几分钟)

奇怪的是,记录本身似乎保存得很好,我只是把错误拿回来

有人能看到我的代码有问题吗,或者这是其他问题吗

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]); 
…在异步插入完成之前,因此它会呕吐

然而,我不确定在这种情况下如何做出回应。请看我的新问题: