skip to Main Content

I’m using the ebay-api for node.js and when I refresh the page for some reason I get an error in the console:

can’t send headers after they are sent

Here is my code, can anyone help me understand why I am getting the error when refreshing the page?

// example simple request to FindingService:findItemsByKeywords

var ebay = require('../index.js');
var http = require('http');

var express = require('express');
var app = express();
var io = require('socket.io');


app.set('port', process.env.PORT || 5000);

app.get('/getEbay', function (req, res) {
    console.log('inside get');
    //  for avoiding crossbrowser-error
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Content-Type', 'application/json');
    var params = {};

    params.keywords = ["cat"];
    params['paginationInput.entriesPerPage'] = 10;
    ebay.ebayApiGetRequest({
        serviceName: 'FindingService',
        opType: 'findItemsByKeywords',
        appId: 'MYAPPID',      // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI
        params: params,
        // filters: filters,
        parser: ebay.parseItemsFromResponse    // (default)
    },
    // gets all the items together in a merged array
  function ebayApiGetRequest(error, items) {
      if (error) throw error;

      console.log('Found', items.length, 'items');
      //  res.send(items);
        console.log(JSON.stringify(items));


      res.contentType('application/json');

      res.send(JSON.stringify(items));

      //  }  
  }
);

});


http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

console.log('Listening on port 5000...');

I think that my mistake may be in the order for calling the functions or maybe there is a way to avoid this wrong calling?

3

Answers


  1. Chosen as BEST ANSWER

    well guys we figured out the problem !

    Here's the new code that is working :

    // example simple request to FindingService:findItemsByKeywords
    var http = require('http');
    
    var express = require('express');
    var app = express();
    var io = require('socket.io');
    var ebay = require('../index.js');
    
    
    app.set('port', process.env.PORT || 5000);
    
    app.get('/getEbay', function(req, res) {
        console.log('inside get');
        //  for avoiding crossbrowser-error
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
        res.header('Content-Type', 'application/json');
    
        getEbaybyResults(function(error, items) {
              if (error) throw error;
    
              console.log('Found', items.length, 'items');
              //  res.send(items);
              console.log(JSON.stringify(items));
    
    
            // res.contentType('application/json');
            //  console.log(try);
           // res.send(JSON.stringify(items));
            res.end(JSON.stringify(items));
        });
    
    
    });
    
    function getEbaybyResults(callback) {
    
       var params = {};
    
        params.keywords = ["cat"];
        params['paginationInput.entriesPerPage'] = 10;
        ebay.ebayApiGetRequest(
          {
            serviceName: 'FindingService',
            opType: 'findItemsByKeywords',
            appId: 'MYAPPID',      // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI
            params: params,
            // filters: filters,
            parser: ebay.parseItemsFromResponse    // (default)
          },
          function(error, items) {
            callback(error, items);
          }
        );
        //ebayApiGetRequest();
    }
    
    http.createServer(app).listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });
    
    //console.log('Listening on port 5000...');
    

  2. function ebayApiGetRequest(error, items) {
          if (error) throw error;
    
          console.log('Found', items.length, 'items');
          //  res.send(items);
            console.log(JSON.stringify(items));
    
    
          res.contentType('application/json');
    
          res.send(JSON.stringify(items));// you send here
          res.send(items);//and you send again right afterwards?
    
          //  }  
      }
    

    If you actually execute send twice, then of course you’re going to send the data twice. One of those res.send() is enough.

    Login or Signup to reply.
  3. Instead of this:

    res.send(JSON.stringify(items));
    res.send(items);
    

    Try for:

    res.end(JSON.stringify(items));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search