在Node JS中发送后,无法设置标头

时间:2016-04-23 08:49:55

标签: javascript node.js mongodb

发送后无法设置标题 获取错误发送后无法设置标题..如何解决此错误

app.post('/post', function (req, res) {

var diagnosis;

var obj = req.headers['data'];

var temp = JSON.parse(obj)
console.log("Received body data:");
console.log(obj);

MongoClient.connect(url, function (err, db) {
    assert.equal(null, err);
    insertDocument(temp, db, function () {
        db.close();
    });
});


var insertDocument = function (obj, db, callback) {
    try {            
        console.log(obj);
        db.collection('diagnosis').remove();
        db.collection('diagnosis').insertOne(obj, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted a document into the Diagnosis collection.");
            callback();
        });
        res.send("Inserted a document into the Diagnosis collection.");
    } catch (err) {
        console.log(err);
    }
};

res.send('Hello POST');})

我调用API的函数是

$scope.post = function () {

                var diagnosis = $scope.Treatments;


            var httpRequest = $http({
                method: 'POST',
                url: '/post',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'data': JSON.stringify($scope.Treatments)
                },

            }).success(function (data, status) {                    
            });
        };

1 个答案:

答案 0 :(得分:1)

此错误表示:您已发送回复。我希望你阅读this。在对请求执行操作后,当您向您发送响应时,连接将被删除。