{"action":"created","issue":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/1371","labels_url":"https://api.github.com/repos/keystonejs/keystone/issues/1371/labels{/name}","comments_url":"https://api.github.com/repos/keystonejs/keystone/issues/1371/comments","events_url":"https://api.github.com/repos/keystonejs/keystone/issues/1371/events","html_url":"https://github.com/keystonejs/keystone/issues/1371","id":74032331,"number":1371,"title":"res.setHeader  failed  Can't set headers after they are sent.","user":{"login":"arthurtalkgoal","id":7088330,"avatar_url":"https://avatars.githubusercontent.com/u/7088330?v=3","gravatar_id":"","url":"https://api.github.com/users/arthurtalkgoal","html_url":"https://github.com/arthurtalkgoal","followers_url":"https://api.github.com/users/arthurtalkgoal/followers","following_url":"https://api.github.com/users/arthurtalkgoal/following{/other_user}","gists_url":"https://api.github.com/users/arthurtalkgoal/gists{/gist_id}","starred_url":"https://api.github.com/users/arthurtalkgoal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/arthurtalkgoal/subscriptions","organizations_url":"https://api.github.com/users/arthurtalkgoal/orgs","repos_url":"https://api.github.com/users/arthurtalkgoal/repos","events_url":"https://api.github.com/users/arthurtalkgoal/events{/privacy}","received_events_url":"https://api.github.com/users/arthurtalkgoal/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-05-07T15:43:39Z","updated_at":"2015-05-09T15:26:03Z","closed_at":null,"body":"Hi ,\r\n\r\nI want to generate ***.vcf*** (vcard) file of the company in my database.\r\n\r\nHowever, if I update the ```res.setHeader``` after my grabbing the company, it has error *** Can't set headers after they are sent.***\r\n\r\nIf I put the setheader to top, I cannot do the ***company not found*** validation.\r\n\r\nAlso, My failname cannot rely on the company.\r\n\r\nWhere should I update the set header?  \r\n```\r\nvar Company = keystone.list('Company');\r\n\r\nexports = module.exports = function(req, res) {\r\n  ...\r\n\r\n\t// Load the current post\r\n\tview.on('init', function(next) {\r\n    Company.model\r\n      .findOne({slug: req.params.id})\r\n        .exec(function(err, company) {\r\n          if (company) {\r\n\r\n              locals.company = company;\r\n              email = company.email                    \r\n              \r\n              bizcard = [\"BEGIN:VCARD\"];\r\n              bizcard.push(\"VERSION:2.1\");\r\n              bizcard.push(\"N:Arthur;CHAN\");  // name\r\n              bizcard.push(\"FN:Arthur Chan\");  // Full Name\r\n              bizcard.push(\"ORG:\" + company.name);\r\n              bizcard.push(\"TEL;WORK;VOICE:\" + company.phone);\r\n              bizcard.push(\"ADR;WORK:;;\" + company.address.serialized);\r\n              bizcard.push(\"EMAIL;PREF;INTERNET:\" + email);\r\n              bizcard.push(\"BEGIN:VCARD\")\r\n\r\n              res.send(bizcard.join(\"\\n\"));\r\n              \r\n\r\n             /*\r\n              * Failed to set Header here!\r\n             **************/\r\n              res.setHeader('Content-Type', 'application/octet-stream');\r\n              res.setHeader('Content-disposition', 'attachment; filename=\"'+company.slug+'.vcf\"');\r\n              \r\n\r\n\r\n              next(err);\r\n\r\n            } else {\r\n              req.flash('error', req.i18n.__(\"error.company_notfound\")) ;\r\n              return res.redirect('/shops' );\r\n\r\n            }\r\n          });\r\n\t});\r\n\r\n  view.render('shops/businesscard');\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/keystonejs/keystone/issues/comments/100506078","html_url":"https://github.com/keystonejs/keystone/issues/1371#issuecomment-100506078","issue_url":"https://api.github.com/repos/keystonejs/keystone/issues/1371","id":100506078,"user":{"login":"creynders","id":436347,"avatar_url":"https://avatars.githubusercontent.com/u/436347?v=3","gravatar_id":"","url":"https://api.github.com/users/creynders","html_url":"https://github.com/creynders","followers_url":"https://api.github.com/users/creynders/followers","following_url":"https://api.github.com/users/creynders/following{/other_user}","gists_url":"https://api.github.com/users/creynders/gists{/gist_id}","starred_url":"https://api.github.com/users/creynders/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/creynders/subscriptions","organizations_url":"https://api.github.com/users/creynders/orgs","repos_url":"https://api.github.com/users/creynders/repos","events_url":"https://api.github.com/users/creynders/events{/privacy}","received_events_url":"https://api.github.com/users/creynders/received_events","type":"User","site_admin":false},"created_at":"2015-05-09T15:26:03Z","updated_at":"2015-05-09T15:26:03Z","body":"@arthurtalkgoal Have you tried my suggestion?"}}