Javascript 设置标题后不能设置标题
我试图从我保存在neo4j数据库中的Mongo中获取ids,并在mongodb中搜索这些id以返回我想要的对象。它工作了1次,但是我的服务器崩溃了,它返回一个错误,即发送邮件后无法设置邮件头 这是我的代码:Javascript 设置标题后不能设置标题,javascript,node.js,mongodb,neo4j,Javascript,Node.js,Mongodb,Neo4j,我试图从我保存在neo4j数据库中的Mongo中获取ids,并在mongodb中搜索这些id以返回我想要的对象。它工作了1次,但是我的服务器崩溃了,它返回一个错误,即发送邮件后无法设置邮件头 这是我的代码: routes.get('/advertisements/recommended/:brand', function(req, res) { res.contentType('application/json'); var brandFromUrl = req.params.bran
routes.get('/advertisements/recommended/:brand', function(req, res) {
res.contentType('application/json');
var brandFromUrl = req.params.brand;
var advertisementIds = [];
Advertisement.find({'car.brand': brandFromUrl})
.then(function (ads) {
// res.status(200).json(ads);
ads.forEach(function (record) {
console.log('ids: ' + record._id)
session
.run("MERGE(a:Advertisement {idFromMongo: {idParam}, brand: {brandParam}}) WITH a MATCH(b: Advertisement {brand: {brandParam}}) MERGE(a)-[:SHARED_BRAND]->(b)", {idParam: record._id.toString(), brandParam: brandFromUrl})
.then(function(result) {
session
.run("MATCH (n:Advertisement{brand: {brandParam}}) RETURN (n)", {brandParam: brandFromUrl})
.then(function(result) {
result.records.forEach(function(record){
advertisementIds.push(record._fields[0].properties.idFromMongo);
});
Advertisement.find({
'_id': { $in: advertisementIds}
}, function(err, docs){
res.status(200).json(docs);
})
})
})
});
})
.catch((error) => {
res.status(400).json(error);
});
});
这是我的错误:
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:494:11)
at ServerResponse.setHeader (_http_outgoing.js:501:3)
at ServerResponse.header (c:\dev\individueel-project\individueel-database\node_modules\express\lib\response.js:767:10)
at ServerResponse.send (c:\dev\individueel-project\individueel-database\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (c:\dev\individueel-project\individueel-database\node_modules\express\lib\response.js:267:15)
at c:\dev\individueel-project\individueel-database\api\advertisement.routes.v1.js:70:35
at model.Query.<anonymous> (c:\dev\individueel-project\individueel-database\node_modules\mongoose\lib\model.js:4046:16)
at c:\dev\individueel-project\individueel-database\node_modules\kareem\index.js:273:21
at c:\dev\individueel-project\individueel-database\node_modules\kareem\index.js:131:16
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
错误:发送头后无法设置头。
在validateHeader(_http_outgoing.js:494:11)
在ServerResponse.setHeader(_http_outgoing.js:501:3)
在ServerResponse.header(c:\dev\individuel project\individuel database\node\u modules\express\lib\response.js:767:10)
在ServerResponse.send(c:\dev\individuel project\individuel database\node\u modules\express\lib\response.js:170:12)
在ServerResponse.json(c:\dev\individuel project\individuel database\node\u modules\express\lib\response.js:267:15)
在c:\dev\individuel project\individuel database\api\advision.routes.v1.js:70:35
在model.Query。(c:\dev\individuel project\individuel database\node\u modules\mongoose\lib\model.js:4046:16)
在c:\dev\individuel project\individuel database\node\u modules\kareem\index.js:273:21
在c:\dev\individuel project\individuel database\node\u modules\kareem\index.js:131:16
at _combinedTickCallback(内部/流程/下一步_tick.js:131:7)
在进程中。_tick回调(内部/process/next_tick.js:180:9)
这里的问题是,您正在执行一个数组,并将响应发送到该数组中。因此,它最终会发送响应几次,但只有第一次有效,之后会引发错误。
要解决此问题,您应该在数组中的所有承诺完成(Promise.all)后回答请求,或者如果您不需要等到整个数组完成,请检查您是否已经响应,并且不要再次响应。每个请求只能发送一次响应。但在您的代码中,您正在为语句内部发送多个响应