Я читаю файл json для запроса с помощью req.params и хочу вернуть res.json, но получаю ошибку из express.js
_http_outgoing.js:482
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
в route.js
module.exports = (app) => {
app.get('/find/:province', province.find);
}
в controller.js
exports.find = function (req, res) {
fs.readFile('./app/models/province_file.json', function(err, provinces){
if (err) throw err;
let obj_provinces = JSON.parse(provinces);
let province = req.params.province;
for (var i = 0; i < obj_provinces.features.length; i++) {
if (obj_provinces.features[i].properties.tb_tn == province) {
let obj_result = obj_provinces.features[i].properties;
console.log(obj_result);
res.json(obj_result);
}
}
});
}
когда я запускаю код, я получаю ошибку
_http_outgoing.js:482
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:482:11)
at /Users/workspace/app/controllers/controller.js:87:13
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:54:3)