Search code examples
node.jsebay-api

nodejs application is looping sort of ebay API


This code is intended to:

  • Accept data via a web front end
  • Feed it back to a nodejs server
  • The node server uses the web front end's data as a search parameter for EBay's API
  • The data sent back from ebay is then fed back to the web front end

The problem: The first time I run the search it works fine. The second time I run the search it spits the same data out on the web front end twice. The third, four times. And so on.

I "think" the problem is here:

//Thus begins the loop
    ebay.ebayApiGetRequest({ 
      serviceName: 'FindingService', 
      opType: 'findItemsByKeywords', 
      appId: ,
      params: params, 
      filters: filters, 
    parser: ebay.parseItemsFromResponse
    }, 
// gets all the items together in a merged array
    function itemsCallback(error, items) { 
      if (error) throw error; 

      console.log('Found', items.length, 'items'); 

      for (var i = 0; i < items.length; i++) { 
        console.log('- ' + items[i].title);
        io.emit('incomingdata', '- ' + items[i].title );
    } 
    console.log('Inside ebayApiGetRequest: ', ++iteration), io.emit('incomingdata', 'Inside ebayApiGetRequest: ' + iteration)     
   }
 ); 

My output

Outside ebayApiGetRequest: 1
Search data was: send
Found 2 items
- Nike Dunk High Pro SB SEND HELP 8.5
- Mix 50PCS Rubber Charms For Rainbow Loom Bands for bracelet/Randomly send###
Inside ebayApiGetRequest: 1
Outside ebayApiGetRequest: 1
Search data was: test 2
Found 2 items
- Iron Man (Blu-ray Disc, 2008, 2-Disc Set, Ultimate Edition) w/ Slip Cover Tested
- 100 One Touch Ultra Blue Test Strips 2 Boxes 50 Count
Inside ebayApiGetRequest: 2
Found 2 items
- Iron Man (Blu-ray Disc, 2008, 2-Disc Set, Ultimate Edition) w/ Slip Cover Tested
- 100 One Touch Ultra Blue Test Strips 2 Boxes 50 Count
Inside ebayApiGetRequest: 1
Outside ebayApiGetRequest: 1
Search data was: third search
Found 2 items
- Germany Nazi Third Reich Nazi Soldiers Search Light 10+5 Stamp WW2 Era
- Vintage Star Wars yellow 3rd series #179 Stormtroopers search the...Topps 1977
Inside ebayApiGetRequest: 3
Found 2 items
- Germany Nazi Third Reich Nazi Soldiers Search Light 10+5 Stamp WW2 Era
- Vintage Star Wars yellow 3rd series #179 Stormtroopers search the...Topps 1977
Inside ebayApiGetRequest: 2 Found 2 items
- Germany Nazi Third Reich Nazi Soldiers Search Light 10+5 Stamp WW2 Era
- Vintage Star Wars yellow 3rd series #179 Stormtroopers search the...Topps 1977
Inside ebayApiGetRequest: 1

Full code follows.

The nodejs server:

var request = require('request');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var ebay = require('ebay-api'); 

//This defines what main page to load

app.get('/', function(req, res){
  res.sendfile('engine.html');
});

// Simply lets us know when users connect and disconnect
io.on('connection', function(socket){
  console.log('a user connected');
    socket.on('disconnect', function(){
      console.log('user disconnected');
});

//When data comes in do the following
  socket.on('incomingdata', function(msg){

//Check to make sure msn is not null
    if (msg != ''){

// example simple request to FindingService:findItemsByKeywords 

        var params = {}; 
        params.keywords = [ msg ]; 

// add additional fields 
        params.outputSelector = [ 'AspectHistogram' ]; 
        params['paginationInput.entriesPerPage'] = 10; 

        var filters = {}; 

        filters.itemFilter = [ 
          new ebay.ItemFilter("FreeShippingOnly", true) 
        ]; 

        filters.domainFilter = [ 
          new ebay.ItemFilter("domainName", "Digital_Cameras") 
        ]; 

        ebay.ebayApiGetRequest({ 
          serviceName: 'FindingService', 
          opType: 'findItemsByKeywords', 
          appId: 'Removed for security',      // 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 itemsCallback(error, items) { 
        if (error) throw error; 

        console.log('Found', items.length, 'items'); 

        for (var i = 0; i < items.length; i++) { 
          console.log('- ' + items[i].title);
          io.emit('incomingdata', '- ' + items[i].title ); 
        }   
        itemsCallback = [];
      }); 

//put the data back to the client and console
      console.log('Search data was: ', msg); 
      io.emit('incomingdata',  msg);

//This ends the process and outputs error message if the data was not entered
} else {
    console.log ('No data entered');
    io.emit('incomingdata','No data entered');

};
            });
});


http.listen(3000, function(){
  console.log('listening on *:3000');
});

This is the front end

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>The interface heading</title>
  </head>

<!-- Update this junk with CSS tags -->

<body>
  <div id="container" style="width:1000px">

    <div id="header" style="background-color:#DCDCDC;">
   <ul id="messages"></ul>

    <form action="">
      <input id="m" autocomplete="off" />
      <input id="x" autocomplete="off" />
      <button>Send</button>
    </form>


    </div>

    <div id="menu" class= "scroll" style="background-color:white;height:400px;width:700px;float:left;overflow-x:hidden;overflow-y:auto;">
      <p id="sample"></p>
    </div>

    <div id="content" style="background-color:#CCCCCC;height:400px;width:300px;float:left;">
      Scrolling list
    </div>

    <div id="footer" style="background-color:#DCDCDC;clear:both;text-align:center;">
      Statistics
    </div>
  </div>

<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>

<script>
  var socket = io();
  $('form').submit(function(){
    socket.emit('incomingdata', $('#m').val());
//    socket.emit('test', $('#m').val());
    $('#m').val('');
    return false;
  });

  socket.on('incomingdata', function(msg){
    $('#sample').append($('<li>').text(msg));
    var menu_height = $('#menu').outerHeight();
    $('#menu').scrollTop($('#sample').outerHeight());

//    document.getElementById("results").innerHTML = html.join("");
  });
</script>


</body>
</html>

Solution

  • I finally gave up and emailed the author. He directed me to recent contributions. The issue was within the ebay API client. It was resolved by sgri on on Oct 7, 2013 in the contribution titled "Reduced the scope of the event handler to a single"

    https://github.com/benbuckman/nodejs-ebay-api/commit/af83866596a8aa51e8d74594d07a3156c1766249