Node.js と Async コーディングは初めてです。Node.js で動作するネストされた for ループに相当するものを作成する必要があります。私の質問はここに投稿されたものと非常によく似ていることを理解しています: nodejs でネストされたループが非同期に行われ、次のループは 1 つが完了した後にのみ開始される必要がありますが、その投稿を詳細に見ても、コードを修正できませんでした。
XML フィードを使用しています。「パーサー」は xml2js パッケージを使用します。SQLクエリ(mysqlノードパッケージを使用している)を削除すると、ループは期待どおりに実行されますが、SQLクエリを入れると、すべての注文が最初に処理され、「DONE」が出力されます。その後、最後の注文のアイテムを繰り返し検索しようとするため、クエリは失敗します。
for ループを async.forEach ループに置き換えてみましたが、これは役に立ちませんでした。
ノードにとってより慣用的な方法でこれを再コーディングする方法についてのヘルプやアドバイスは大歓迎です。
どうもありがとう!シックスホビット
parser.parseString(data, function (err, result) {
if(err) throw(err);
var numOrders = result['Root']['Orders'][0]['Order'].length;
var curr, currItem, currOrdId, items, sellersCode;
console.log("Logging IDs of", numOrders, "orders");
// for each order
for (var j=0; j<numOrders; j++){
//current order
curr = result['Root']['Orders'][0]['Order'][j];
currOrdId = curr['OrderId'][0]
items = curr['Items'][0]['Item'];
console.log("Order ID:", currOrdId, "--",items.length, "Items");
// for each item
for (var k=0; k<items.length; k++){
currItem = items[k];
sellersCode = currItem['SellersProductCode'][0];
var sqlQuery = 'select data_index, fulltext_id, cataloginventory_stock_item.product_id from catalogsearch_fulltext inner join cataloginventory_stock_item where catalogsearch_fulltext.data_index like "' + sellersCode + '|%"' + 'and cataloginventory_stock_item.item_id = catalogsearch_fulltext.product_id';
var query = connection.query(sqlQuery,function(err,rows,fields){
if (err) throw(err);
console.log(" Item ID :",currItem['ItemId'][0]);
console.log(" Full Text ID :", rows[0]['fulltext_id']);
console.log(" Product ID :", rows[0]['product_id']);
});
}//for
}//for
console.log("DONE");
});//parseString