我想创建一个获取数据的 API。但是,函数 res.json() 根据下面的消息发现了问题web_1 | Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the clientweb_1 | at ServerResponse.setHeader (_http_outgoing.js:533:11)web_1 | at ServerResponse.header (/mbs_welfare/node_modules/express/lib/response.js:771:10)web_1 | at ServerResponse.send (/mbs_welfare/node_modules/express/lib/response.js:170:12)web_1 | at /mbs_welfare/server.js:45:9web_1 | at Layer.handle_error (/mbs_welfare/node_modules/express/lib/router/layer.js:71:5)web_1 | at trim_prefix (/mbs_welfare/node_modules/express/lib/router/index.js:315:13)web_1 | at /mbs_welfare/node_modules/express/lib/router/index.js:284:7web_1 | at Function.process_params (/mbs_welfare/node_modules/express/lib/router/index.js:335:12)web_1 | at next (/mbs_welfare/node_modules/express/lib/router/index.js:275:10)web_1 | at Layer.handle_error (/mbs_welfare/node_modules/express/lib/router/layer.js:67:12)lrouter.get('/profile/user/me', async (req, res, next) => { let user = req.session.user; if(user) { try { let me = user.adminID let info = await knex('admin_accounts').where('adminID', me) res.json(info) } catch (e) { console.log(e); } res.sendFile(path.join(__dirname + '/../public/personal_information.html')); }else{ res.redirect('/'); }});
1 回答
米脂
TA贡献1836条经验 获得超3个赞
出现此错误是因为res
在您的代码中多次调用,即:
res.json(info)
- 时而时
res.sendFile
- 也在这里,例如
您需要确保res
一次只调用一次。
换句话说,response
每个request
.
希望能帮助到你。
添加回答
举报
0/150
提交
取消