eBay API を使用して eBay からリストを取得する NodeJS アプリを作成しました。単純な if ステートメントで除外されるはずの特定のアイテムが通過するという問題があります。
アプリはフロントエンドから投稿データを JSON として受け取り、各検索を実行してから、特定のパラメーターに基づいてアイテムをフィルター処理します。問題のあるコードは次のとおりです。
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);
}
この行if ( price < maxPrice && price > 40 )
は、maxPrice よりも大きく 40 よりも小さいすべてのアイテムを除外する必要があることがわかります。ただし、これは行われません。なぜそれが起こっているのか、ここで何が起こっているのかわかりません。とてもシンプルでわかりやすいように見えますが、そうではありません。これが返されたオブジェクトで、正しく機能していないことがわかります。
111004318957:
listingType: "FixedPrice"
maxPrice: 170
price: 349
ノード クラスターも使用しているため、server.js ファイルには次のように記述されています。
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.");
}
}
ここで何か助けていただければ幸いです。
編集:111004318957
eBay によって返される itemId であることを説明する必要がありました。結果オブジェクトは次のようになります。
results: {
itemId1: {
listingType: '',
maxPrice: '',
price: ''
},
itemId2: {
listingType: '',
maxPrice: '',
price: ''
}
}
編集 2: price
このコード スニペットの前に設定されます。これは eBay の応答で返され、その場所は items[i].listingInfo.listingType に依存するため、簡単な if/else で設定できます。
if ( items[i].listingInfo.listingType == 'AuctionWithBIN' ) {
price = parseInt( items[i].listingInfo.buyItNowPrice.USD );
}
else {
price = parseInt( items[i].sellingStatus.currentPrice.USD );
}