[ERR_HTTP_HEADERS_SENT]:发送数据时出现请求错误

时间:2019-08-23 17:43:35

标签: node.js express airtable

我有一个带有快速后端的react项目。我目前有一个get请求,该请求从airtable在线数据库返回数据。当我向在线数据库发出请求并尝试将其发送回提出请求的前端时,我注意到在将请求发送回之后,我收到以下错误:

_http_outgoing.js:470
    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:470:11)
    at ServerResponse.header (/Users/omarjandali/Desktop/fightcamp/backend/node_modules/express/lib/response.js:771:10)
    at ServerResponse.json (/Users/omarjandali/Desktop/fightcamp/backend/node_modules/express/lib/response.js:264:10)
    at ServerResponse.send (/Users/omarjandali/Desktop/fightcamp/backend/node_modules/express/lib/response.js:158:21)
    at /Users/omarjandali/Desktop/fightcamp/backend/src/server.js:45:11
    at Array.forEach (<anonymous>)
    at /Users/omarjandali/Desktop/fightcamp/backend/src/server.js:31:13
    at /Users/omarjandali/Desktop/fightcamp/backend/node_modules/airtable/lib/query.js:52:9
    at /Users/omarjandali/Desktop/fightcamp/backend/node_modules/airtable/lib/query.js:102:17
    at Request._callback (/Users/omarjandali/Desktop/fightcamp/backend/node_modules/airtable/lib/run_action.js:69:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! backend@1.0.0 start: `node src/server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the backend@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/omarjandali/.npm/_logs/2019-08-23T17_36_14_757Z-debug.log

现在这是服务器中用于获取请求的获取请求...

app.get('/schedules', (req, res) => {
  let data = [];

  base('Filming Date').select({
    view: 'Grid view'
  }).firstPage(function(err, records) {
    if (err) { console.error(err); return; }
    records.forEach(function(record) {
      if(record.get('Public')){
        console.log(record.get('Filming Date'));

        base('Workouts').select({
            view: 'Grid view'
        }).firstPage(function(err, workoutRecords) {
            if (err) { console.error(err); return; }
            workoutRecords.forEach(function(workout) {
            data.push(workout);
            });
            console.log(data);
            res.send(data);
        });
      }
    });
  });

});

它当前发出请求,如果请求成功,则立即向数据库发出次要请求,并且在第二次数据库调用中,数据存储在数组中,并由res.send(data)返回。

不确定如何解决此错误吗?

0 个答案:

没有答案