【问题标题】:Can't set headers after they are sent in my react api在我的反应 api 中发送标头后无法设置标头
【发布时间】: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


    【解决方案1】:

    看起来您在使用 res.json() 时正在用每一行填充响应 见:https://blog.fullstacktraining.com/res-json-vs-res-send-vs-res-end-in-express/

    也许您应该建立响应并发送一次?

    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) You don't need this check as forEach will be not executed if no elements in the array
      //  {
          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)
        // you should not use res.json function on every row as it sends response back
       //           return res.json({investments});
              }
            );
          })
    //    }
    
    
        return res.json({investments});
      });
    };
    

    【讨论】:

      猜你喜欢
      • 2014-08-23
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      相关资源
      最近更新 更多