在节点中更新时发送后无法设置 headers。js/mongoDB

Can't set headers after they are sent when updating in node.js/mongoDB

我是 MEAN 堆栈新手,我正在尝试使用 MongoDB driver for node.js.

在循环中执行多个更新

当尝试通过此调用遍历下面的记录时,我收到“发送后无法设置 headers”,大概是因为 'next' 在每个子序列上被调用并再次调用循环的迭代。

data.sortManual(manualSlug, proposedSortOrder, function(err) {
    if (err) {
        res.send(400, "Failed to update sort order");
    } else {
        res.send(200);
    }
});

如果有人能帮助我理解我做错了什么,我将不胜感激。

sortManual 方法如下:

manualData.sortManual = function(manualSlug, proposedSortOrder, next) {
    database.getDb(function(err, db) {
        if (!err) {
            var arrSortOrder = proposedSortOrder.split(',');

            for (var i = 0; i < arrSortOrder.length; i++) {

                arrSortOrder[i] = arrSortOrder[i].replace(/^\s*/, "").replace(/\s*$/, ""); // trim whitespace

                db.manuals.findAndModify({
                    slug: manualSlug,
                    "topics": {
                        "$elemMatch": {
                            "slug": arrSortOrder[i]
                        }
                    }
                }, [
                    ['_id', 'asc']
                ], {
                    $set: {
                        "topics.$.sort": i
                    }
                }, {
                    new: false,
                    upsert: true
                }, function(err, result) { <-- I probably shouldn't be doing this on each iteration of the loop but where to handle this?
                    if (err) {
                        console.log(err);
                        next(err, null);
                    } else {
                        console.log(result);
                        next(null, result);
                    }
                });
            } // end loop              
        } // end if
    }); // end getDb
}; // end sortManual

与MongoDB无关,与HTTP协议无关

错误消息告诉您 HTTP headers 设置了不止一次,根据协议定义这是不可能的。 (注意:我们谈论的是响应,与 Mongo 内部发生的事情无关).

问题出在回调next(发送headers的那个)执行了多次。

如果您查看您的代码,您会注意到有一个 for 循环,并且 next 在每个循环步骤中用作回调 - 因此我们遇到了问题。

解决方案

您必须重构代码以仅执行一次 next,这可以通过 basic counting example:

来完成
        var counter = arrsortOrder.length;
        var errors = [];
        function checkIfLast(err) {
           if(err)   {
              errors.push(err);
           }
           counter--;
           if(counter == 0)  {
             if(errors.length > 0)
                next(errors.join());
             else
                next();
           }
        }
        for (var i = 0; i < arrSortOrder.length; i++) {

            arrSortOrder[i] = arrSortOrder[i].replace(/^\s*/, "").replace(/\s*$/, ""); // trim whitespace

            db.manuals.findAndModify({
                slug: manualSlug,
                "topics": {
                    "$elemMatch": {
                        "slug": arrSortOrder[i]
                    }
                }
            }, [
                ['_id', 'asc']
            ], {
                $set: {
                    "topics.$.sort": i
                }
            }, {
                new: false,
                upsert: true
            }, function(err, result) { <-- I probably shouldn't be doing this on each iteration of the loop but where to handle this?
                if (err) {
                    checkIfLast(err);
                    console.log(err);
                    next(err, null);
                } else {
                    checkIfLast();
                    console.log(result);
                    next(null, result);
                }