gpt4 book ai didi

node.js - 在 node.js/mongoDB 中更新时发送后无法设置 header

转载 作者:可可西里 更新时间:2023-11-01 10:29:46 24 4
gpt4 key购买 nike

我是一个 MEAN 堆栈新手,正在尝试使用 node.js 的 MongoDB 驱动程序在一个循环中执行多个更新。

当尝试通过此调用遍历下面的记录时,我收到“发送后无法设置标题”,大概是因为在循环的每个后续迭代中调用并再次调用“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协议(protocol)无关。

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

问题在于回调next(发送 header 的回调)被执行了多次。

如果您查看您的代码,您会注意到有一个 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);
}

关于node.js - 在 node.js/mongoDB 中更新时发送后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28341394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com