ColdFusion - 位置X处的数组元素无法找到

时间:2013-11-28 16:07:25

标签: arrays for-loop coldfusion struct

我在函数中有以下脚本偶尔会抛出“找不到位置X的元素”错误,即使脚本在脚本中先前使用该元素位置调用了某些东西。 (例如,我可能会在调用Player.getPlayerBIN函数时遇到与位置X处的元素无关的错误,尽管它会在其上方调用Player.getPlayerRecord函数而没有任何问题。

// IF 1 OR MORE RESULT RETURNED
if (LOCAL.numResults GT 0) {

    // LOOP ROUND RESULTS
    for (i=1; i <= LOCAL.numResults; i++) {

        // DEFAULT VARIABLES
        LOCAL.checkResourceID = '';
        LOCAL.insResource = '';
        LOCAL.getPlayerBIN = '';
        LOCAL.lowestBIN = 0;
        LOCAL.basePrice = 0;
        LOCAL.biddingPrice = 0;
        LOCAL.bidAmount = 0;
        LOCAL.binPrice = 0;
        LOCAL.startPrice = 0;
        LOCAL.timeLeft = 0;
        LOCAL.minsLeft = -1;
        LOCAL.auctionEnds = '';
        LOCAL.checkBids = '';
        LOCAL.insStuff = '';
        LOCAL.buyCard = '';
        LOCAL.bidCard = '';

        // CHECK IF RECORD EXISTS FOR RESOURCE ID
        LOCAL.checkResourceID = Application.cfcs.Player.getPlayerRecord(LOCAL.searchResults.auctionInfo[i].itemData.resourceID);
        if (LOCAL.checkResourceID.RecordCount EQ 0) {
                         LOCAL.insResource = Application.cfcs.Player.insPlayerResource(LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                         LOCAL.searchResults.auctionInfo[i].itemData.assetID,
                         LOCAL.searchResults.auctionInfo[i].itemData.rating,
                         LOCAL.searchResults.auctionInfo[i].itemData.rareFlag,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[1].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[2].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[3].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[4].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[5].value,
                         LOCAL.searchResults.auctionInfo[i].itemData.attributeList[6].value);   
        }

        // QUERY PLAYER BIN PRICE DATA
        LOCAL.getPlayerBIN = Application.cfcs.Player.getPlayerBIN(LOCAL.searchResults.auctionInfo[i].itemData.resourceID);

        // IF LOWEST BIN RECORD EXISTS
        if (LOCAL.getPlayerBIN.RecordCount GT 0) {

            // SET LATEST LOWEST BIN PRICE
            LOCAL.lowestBIN = LOCAL.getPlayerBIN.lowestBIN;

            // SET BUYING & SELLING PRICES
            //if (LOCAL.lowestBIN GT 3000) {
            //  LOCAL.basePrice = LOCAL.lowestBIN * 0.9;
            //} else {
                LOCAL.basePrice = LOCAL.lowestBIN * 0.8;
            //}
            if (LOCAL.lowestBIN LT 1000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 50) * 50;
                LOCAL.binPrice = LOCAL.lowestBIN - 50;
                LOCAL.startPrice = LOCAL.binPrice - 50;
            } else if (LOCAL.lowestBIN LT 10000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 100) * 100;
                LOCAL.binPrice = LOCAL.lowestBIN - 100;
                LOCAL.startPrice = LOCAL.binPrice - 100;
            } else if (LOCAL.lowestBIN LT 50000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 250) * 250;
                LOCAL.binPrice = LOCAL.lowestBIN - 250;
                LOCAL.startPrice = LOCAL.binPrice - 250;
            } else if (LOCAL.lowestBIN LT 100000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 500) * 500;
                LOCAL.binPrice = LOCAL.lowestBIN - 500;
                LOCAL.startPrice = LOCAL.binPrice - 500;
            } else if (LOCAL.lowestBIN GTE 100000) {
                LOCAL.biddingPrice = Round(LOCAL.basePrice / 1000) * 1000;
                LOCAL.binPrice = LOCAL.lowestBIN - 1000;
                LOCAL.startPrice = LOCAL.binPrice - 1000;
            }
        }

        // IF BIN PRICE SET AND IS LESS THAN SET BUYING PRICE
        if (LOCAL.searchResults.auctionInfo[i].buyNowPrice GT 0 AND LOCAL.searchResults.auctionInfo[i].buyNowPrice LTE LOCAL.biddingPrice) {
            LOCAL.timeLeft = LOCAL.searchResults.auctionInfo[i].expires;
            LOCAL.auctionEnds = DateAdd("s",LOCAL.timeLeft,Now());
            LOCAL.insStuff = Application.cfcs.Bid.insStuff(
                               LOCAL.resourceID,
                               LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                               LOCAL.getPlayerBIN.resourceID,
                               LOCAL.searchResults.auctionInfo[i].itemData.preferredPosition,
                               LOCAL.searchResults.auctionInfo[i].itemData.playStyle,
                               LOCAL.lowestBIN,
                               LOCAL.searchResults.auctionInfo[i].startingBid,
                               LOCAL.searchResults.auctionInfo[i].buyNowPrice,
                               LOCAL.searchResults.auctionInfo[i].currentBid,
                               LOCAL.basePrice,
                               LOCAL.biddingPrice,
                               LOCAL.bidAmount,
                               LOCAL.startPrice,
                               LOCAL.binPrice);

        // ELSE IF LISTING START PRICE & CURRENT BID LESS THAN BIDDING PRICE
        } else if (LOCAL.searchResults.auctionInfo[i].startingBid LT LOCAL.biddingPrice AND LOCAL.searchResults.auctionInfo[i].currentBid LT LOCAL.biddingPrice) {
            if (LOCAL.searchResults.auctionInfo[i].currentBid LT LOCAL.searchResults.auctionInfo[i].startingBid) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].startingBid; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 1000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 50; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 10000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 100; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 50000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 250; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid LT 100000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 500; }
            else if (LOCAL.searchResults.auctionInfo[i].currentBid GTE 100000) { LOCAL.bidAmount = LOCAL.searchResults.auctionInfo[i].currentBid + 1000; }
            LOCAL.timeLeft = LOCAL.searchResults.auctionInfo[i].expires;
            LOCAL.auctionEnds = DateAdd("s",LOCAL.timeLeft,Now());
            LOCAL.minsLeft = DateDiff("n",now(),LOCAL.auctionEnds);
            if (LOCAL.minsLeft LTE 25 AND LOCAL.bidAmount LT LOCAL.lowestBIN) {
                LOCAL.checkBids = Application.cfcs.Bid.getBids(LOCAL.searchResults.auctionInfo[i].itemData.ID);
                if (LOCAL.checkBids.RecordCount EQ 0) {
                    LOCAL.insStuff = Application.cfcs.Bid.insStuff(
                                               LOCAL.resourceID,
                                             LOCAL.searchResults.auctionInfo[i].itemData.resourceID,
                                               LOCAL.getPlayerBIN.resourceID,
                                           LOCAL.searchResults.auctionInfo[i].itemData.preferredPosition,
                                               LOCAL.searchResults.auctionInfo[i].itemData.playStyle,
                                               LOCAL.lowestBIN,
                                               LOCAL.searchResults.auctionInfo[i].startingBid,
                                               LOCAL.searchResults.auctionInfo[i].buyNowPrice,
                                               LOCAL.searchResults.auctionInfo[i].currentBid,
                                               LOCAL.basePrice,
                                               LOCAL.biddingPrice,
                                               LOCAL.bidAmount,
                                               LOCAL.startPrice,
                                               LOCAL.binPrice);

                }
            }
        }
    }
}

任何人都有任何关于可能会发生什么的想法或建议,因为我在谷歌搜索中找不到:(

由于

2 个答案:

答案 0 :(得分:3)

我不知道彼得为什么不回答这个问题,所以我愿意。

问题几乎可以肯定是因为你不是VAR - 确定你的循环计数器变量i。如果它所在的CFC实例位于共享范围内,那么使用该函数的所有请求将同时共享/更改相同的i变量,这将使您的代码沿着您所看到的线路不稳定。 / p>

始终VAR您的函数局部变量,或在引用它们时指定本地范围。一直以来。

答案 1 :(得分:0)

LOCAL.numResults真的不需要

建议1

使用arraylen

for (i=1; i <= ArrayLen(LOCAL.searchResults.auctionInfo[i]); i++) {
   ...
   } 

这仅适用于您的数组为1到ArrayLen()

的情况

建议2

使用for-in

for (local.myAuctionInfo in LOCAL.searchResults.auctionInfo) {
   ...
   }

请参阅For in loop

即使您的数组在其所有索引中都没有数据

,这也会有效