[英]'ERR_HTTP_HEADERS_SENT' after POST request in Node.js/ Express app with request http client
The line res.send("Successfully saved the new address.");
res.send("Successfully saved the new address.");
throws the 'ERR_HTTP_HEADERS_SENT'
error.抛出
'ERR_HTTP_HEADERS_SENT'
错误。
I read through other posts concerning the same error and tried return res.send("Successfully saved the new address.")
, but that doesn't fix it.我通读了有关相同错误的其他帖子并尝试
return res.send("Successfully saved the new address.")
,但这并没有解决它。 Any insights?任何见解?
Note: I am new to this.注意:我是新手。
Please be kind.请善待。
Thanks.谢谢。
My Code我的代码
app.post("/", function(req, res) {
const url = "http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?";
const street = req.body.street;
const city = req.body.city;
const state = req.body.state;
const zip = req.body.zip;
const yourAddress = "Address=" + street.replace(" ", "+") + "&City=" + city.replace(" ", "+") + "&Zip=" + zip;
const parameters = "&category=&outFields=*&forStorage=false&f=json";
request(url + yourAddress + parameters, function(error, response, body) {
const data = JSON.parse(body);
const newAddress = data.candidates[0].address;
const longitude = data.candidates[0].location.x;
const latitude = data.candidates[0].location.y;
const address = new Address({
address: newAddress,
latitude: latitude,
longitude: longitude
});
address.save(function(err) {
if (!err) {
res.send("Successfully saved the new address.");
} else {
res.send(err);
}
});
});
res.redirect("/");
});
You are doing both res.send()
and res.redirect()
in the same request handler.您在同一个请求处理程序中同时执行
res.send()
和res.redirect()
。 You can't send two responses to the same request.您不能对同一个请求发送两个响应。 That's what generates the warning message you see.
这就是生成您看到的警告消息的原因。
Pick one of the other.选择其中之一。 You either want to send an appropriate status about the
.save()
or you want to redirect.您要么想发送有关
.save()
的适当状态,要么想重定向。 One or the other, not both.一个或另一个,而不是两者。
In this particular code, the res.redirect()
happens first so it's what the client sees.在这个特定的代码中,
res.redirect()
首先发生,所以它是客户端看到的。 Then, after that and when the request()
and the address.save()
have both completed, then you try to res.send()
, but that is blocked because a response has already been sent and the http connection is already done.然后,在那之后,当
request()
和address.save()
都完成时,然后您尝试res.send()
,但由于已经发送了响应并且已经完成了http连接,因此被阻止。
If you just want to redirect upon successful save, you can remove the existing res.redirect()
and change to this:如果您只想在成功保存后重定向,您可以删除现有的
res.redirect()
并更改为:
address.save(function(err) {
if (!err) {
res.redirect("/");
} else {
console.log(err);
res.sendStatus(500);
}
I would Agree With @jfriend00 answer , since there can be only one response for the request .我同意@jfriend00 的回答,因为请求只能有一个响应。 Here's what you can do:
您可以执行以下操作:
address.save(function(err) {
if (!err) {
res.redirect('/success')
} else {
res.send(err);
}
});
//add a new route
app.get('/success',(req,res)=>{res.send('Successfully saved the new address.')})
And then you can redirect to the home page.然后你可以重定向到主页。
This is just a workaround in this case and can differ logically in each case depending on the requirement在这种情况下,这只是一种解决方法,并且在每种情况下都可能在逻辑上有所不同,具体取决于需求
The Other Way is to redirect on the client side itself eg:If you're on jquery另一种方法是在客户端本身重定向,例如:如果您使用 jquery
$.ajax({
statusCode: {
200: function() {
$(location).attr('href', 'to the homepage')
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.