【发布时间】:2020-01-05 14:30:50
【问题描述】:
当我调用我的 api 时,我收到错误 'Can\'t set headers after they are sent. 任何人都可以帮助我为什么会收到此错误?在这里,我已经添加了该 api 的整个代码,任何人都可以查看它并帮助我解决其中的确切问题吗?
API:
exports.getInvestments = (req, res) => {
console.log('getInvestments');
mysql_client.query(`SELECT * FROM connects WHERE user_id = '${req.body.user_id}' `, (err, rows) => {
if(err) {
return res.json(err);
}
console.log(rows);
let investments = [];
if(rows.length > 0)
{
rows.forEach((row) => {
const ACCESS_TOKEN = row.access_token;
// Pull transactions for the last 30 days
let startDate = moment()
.subtract(30, "days")
.format("YYYY-MM-DD");
let endDate = moment().format("YYYY-MM-DD");
client.getInvestmentTransactions(
ACCESS_TOKEN,
startDate,
endDate,
{
count: 250,
offset: 0
},
function(err, result) {
if(err)
{
console.log('Investment error');
return res.json(err);
}
console.log("Get all investments");
console.log(result.investment_transactions);
investments = investments.concat(result.investment_transactions)
return res.json({investments});
}
);
})
}
else
return res.json({investments});
});
};
【问题讨论】:
标签: node.js reactjs node-modules