skip to Main Content

I wrote a NodeJS app that uses eBay API to get listings from eBay. I’m having an issue where certain items are passing through even though they are supposed to be filtered out with a simple if statement.

The app receives post data from the front end as JSON, executes each search and then filters items out based on certain params. Here is the offending code:

if ( items[i].listingInfo.listingType != 'Auction' ) {
    //console.log( items[i].listingInfo.listingType );

    if ( items[i].primaryCategory.categoryId == '9355' ) {
        //console.log( items[i].primaryCategory.categoryId );

        if ( price < maxPrice && price > 40 ) {
            //console.log( price, maxPrice );
            file =  path + 
                    items[i].itemId + '-' + 
                    price + '-' + maxPrice + '-' + 
                    items[i].primaryCategory.categoryId + '-' + 
                    items[i].listingInfo.listingType;

            if ( !fs.existsSync( file ) ) {
                console.log(
                    'File ' + file + ' does not exist.', 
                    !fs.existsSync( file ), 
                    items[i].listingInfo.listingType, 
                    price < maxPrice,
                    items[i].itemId
                );

                fs.writeFile( file, ' ', function(err) {
                    if (err) {
                        if (debug)
                            console.log('Writing ' + file + ' failed.');
                    }
                    else {
                        if (debug)
                            console.log('Writing ' + file + ' worked.');      
                        returnData.success = true;
                        returnData.results[ result.itemId ] = result;
                        console.log( price, maxPrice, !fs.existsSync( file ) );
                        console.log('success');
                    }
                })
            }
            else {
                returnData.discard.file[ result.itemId ] = result;
                delete returnData.results[ result.itemId ];
            }

        }
        else {
            returnData.discard.price[ result.itemId ] = result;
            if (debug)
                console.log('FAILED (price): ' + items[i].itemId + ' is ' + ( price - maxPrice ) + ' greater than maxPrice.');
        }
    }
    else {
        returnData.discard.cat[ result.itemId ] = result;
        if (debug)
            console.log('FAILED (categoryId): ' + items[i].itemId + ' is ' + items[i].primaryCategory.categoryId);
    }                    
}
else {
    returnData.discard.type[ result.itemId ] = result;
    if (debug)
        console.log('FAILED (listingType): ' + items[i].itemId + ' is a ' + items[i].listingInfo.listingType);                
}

You can see this line if ( price < maxPrice && price > 40 ) should filter out any items that are greater than the maxPrice and lower than 40. However, it does not do this. I have no idea why it’s happening and what is going on here. It seems very simple and straightforward but isn’t. Here is the returned object where you can see that it’s not working properly.

111004318957:
    listingType: "FixedPrice"
    maxPrice: 170
    price: 349

I’m also using node clusters, so my server.js file has this:

    function start(route, handle) {
        if ( cluster.isMaster ) {

            for ( var i = 0; i < numCPUs; i++ ) {
                cluster.fork();
            }

            cluster.on('exit', function( worker, code, signal) {
                console.log( 'worker ' + worker.process.pid + ' died' );
            })
        }
        else {
            function onRequest(request, response) {
                var postData = "";
                var pathname = url.parse(request.url).pathname;

                request.setEncoding("utf8");
                request.addListener("data", function(postDataChunk) {
                    postData += postDataChunk;
                });
                request.addListener("end", function() {
                    //console.log('Request ended.');
                    if ( postData != '' ) {
                        postData = JSON.parse(postData);
                    }
                    //console.log(postData.search.searches[0]);
                    route(handle, pathname, response, postData);
                });
            }
            http.createServer(onRequest).listen(8888);
            console.log("Server has started.");
        }
    }

Any help here is appreciated, thanks.

EDIT: I should have explained that the 111004318957 is the itemId that is returned by eBay. The result object looks like this:

results: {
    itemId1: {
        listingType: '',
        maxPrice: '',
        price: ''
    },
    itemId2: {
        listingType: '',
        maxPrice: '',
        price: ''
    }
}

EDIT 2: price is set before this code snippet. It’s returned in eBay’s response and it’s location is dependent on items[i].listingInfo.listingType, so there’s a simple if/else to set that.

if ( items[i].listingInfo.listingType == 'AuctionWithBIN' ) {
    price = parseInt( items[i].listingInfo.buyItNowPrice.USD );
}
else {
    price = parseInt( items[i].sellingStatus.currentPrice.USD );
}

2

Answers


  1. JSON returns listingType, maxPrice, price.

    Try if (items[i].price < maxPrice && items[i].price > 40)

    Login or Signup to reply.
  2. The author will almost certainly not be able to contribute anything to this question, to clarify if my statement is true or not, as it was asked six years ago.

    However, it is fairly certain that the problem has to do with the following part of the code:

    fs.writeFile( file, ' ', function(err) {
      if (err) {
        if (debug)
          console.log('Writing ' + file + ' failed.');
      }
      else {
        if (debug)
          console.log('Writing ' + file + ' worked.');      
        returnData.success = true;
        returnData.results[ result.itemId ] = result;
        console.log( price, maxPrice, !fs.existsSync( file ) );
        console.log('success');
      }
    })
    

    fs.writeFile is async, and if the OP is looping over a list of results, then the result in returnData.results[ result.itemId ] = result will always refer to the last element that loop, no matter if that element matches the condition if ( price < maxPrice && price > 40 ) { or not.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search