getting Error as 'Error: Can't set headers after they are sent' in my node.js service
getting Error as 'Error: Can't set headers after they are sent' in my node.js service
下面是我的代码,
app.get("/getList", function (req, resp) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var client = ldap.createClient({
url: 'ldap://my_ip:389'
});
client.bind('cn=pratik', 'pratik', function (err) {
return resp.send(err);
});
var opts = {
filter: '(objectclass=classToSearch)',
scope: 'sub',
attributes: ['ipaddress']
};
var entries= {};
var i=0;
client.search('o=a', opts, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
oxe[i] = entry.object;
i++;
});
res.on('end', function(result) {
console.log("pratik");
console.log(entries);
return resp.send(entries);
});
});
这里,当我打印条目时,它显示正确的结果,但没有发送响应。我可以将错误视为“错误:发送后无法设置 headers”。下面是完整的错误:
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
我该如何解决这个错误?
这是因为您调用了 resp.send
两次:在 client.bind
回调和 res.on("end")
回调中。
我建议将第二个作业放在 bind
回调中,这样你就可以检查错误,如果有错误,你就停在那里。
如果成功,那么我会发送entries
。
app.get("/getList", function(req, resp) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var client = ldap.createClient({
url: 'ldap://my_ip:389'
});
client.bind('cn=pratik', 'pratik', function(err) {
if (err) return resp.send(err);
var opts = {
filter: '(objectclass=classToSearch)',
scope: 'sub',
attributes: ['ipaddress']
};
var entries = {};
var i = 0;
client.search('o=a', opts, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
oxe[i] = entry.object;
i++;
});
res.on('end', function(result) {
console.log("pratik");
console.log(entries);
return resp.send(entries);
});
});
});
});
下面是我的代码,
app.get("/getList", function (req, resp) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var client = ldap.createClient({
url: 'ldap://my_ip:389'
});
client.bind('cn=pratik', 'pratik', function (err) {
return resp.send(err);
});
var opts = {
filter: '(objectclass=classToSearch)',
scope: 'sub',
attributes: ['ipaddress']
};
var entries= {};
var i=0;
client.search('o=a', opts, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
oxe[i] = entry.object;
i++;
});
res.on('end', function(result) {
console.log("pratik");
console.log(entries);
return resp.send(entries);
});
});
这里,当我打印条目时,它显示正确的结果,但没有发送响应。我可以将错误视为“错误:发送后无法设置 headers”。下面是完整的错误:
_http_outgoing.js:357
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:357:11)
我该如何解决这个错误?
这是因为您调用了 resp.send
两次:在 client.bind
回调和 res.on("end")
回调中。
我建议将第二个作业放在 bind
回调中,这样你就可以检查错误,如果有错误,你就停在那里。
如果成功,那么我会发送entries
。
app.get("/getList", function(req, resp) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var client = ldap.createClient({
url: 'ldap://my_ip:389'
});
client.bind('cn=pratik', 'pratik', function(err) {
if (err) return resp.send(err);
var opts = {
filter: '(objectclass=classToSearch)',
scope: 'sub',
attributes: ['ipaddress']
};
var entries = {};
var i = 0;
client.search('o=a', opts, function(err, res) {
res.on('searchEntry', function(entry) {
console.log('entry: ' + JSON.stringify(entry.object));
oxe[i] = entry.object;
i++;
});
res.on('end', function(result) {
console.log("pratik");
console.log(entries);
return resp.send(entries);
});
});
});
});