TypeError:无法从undefined中读取属性“0” - Javascript

时间:2014-10-30 00:17:01

标签: javascript multidimensional-array

我正在尝试运行以下代码,我收到以下错误:

TypeError: Cannot read property "0" from undefined. (line 111)

有问题的一行是:

perCampaignRows.push([STRATEGISTS[i][1],

这里是完整的代码。

var perCampaignRows = [];

for (var i = 0; i < STRATEGISTS.length; i++) {
    var accountIterator = MccApp.accounts()
        .withCondition("ManagerCustomerId = '" + STRATEGISTS[i][0] + "'")
        .get();

    var mccAccount = AdWordsApp.currentAccount();

    while (accountIterator.hasNext()) {
        var account = accountIterator.next();
        MccApp.select(account);
        Logger.log("Checking " + account.getName() + " - CID: " + account.getCustomerId());
        var campaignIterator = AdWordsApp.campaigns()
            .withCondition("Status = ENABLED")
            .get();

        var activeCampaigns = campaignIterator.totalNumEntities();

        if (activeCampaigns === 0) {
            Logger.log("No Active Campaigns");
            perCampaignRows.push([
                STRATEGISTS[i][1],
                STRATEGISTS[i][2],
                account.getCustomerId(), 
                account.getName(), 
                activeCampaigns, 
                '', 
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
                '',
            ]);
        }
        else {
            Logger.log(activeCampaigns + " Active Campaigns");
        }

        while (campaignIterator.hasNext()) {
            var campaign = campaignIterator.next();
            Logger.log("Campaign: " + campaign.getName());
            var mobileBid = checkPlatform(campaign);
            var isRotatingEvenly = checkAdRotation(campaign);
            var sitelinkData = checkSitelinks (campaign);
            var isUsingMobileAds = checkAds(campaign);
            var callExtData = checkCallExtensions(campaign);
            var isUsingAdSchedule = checkAdSchedules(campaign);
            var isSomewhatLimitedByBudget = checkBudgetLimitations(campaign);

            if (isUsingAdSchedule && isSomewhatLimitedByBudget) {
                var warnAboutBudgetAndSchedule = "TRUE";
            }
            else {
                var warnAboutBudgetAndSchedule = "FALSE";
            }

            Logger.log(perCampaignRows);

            perCampaignRows.push([
                STRATEGISTS[i][1],
                STRATEGISTS[i][2],
                account.getCustomerId(), 
                account.getName(), 
                activeCampaigns, 
                campaign.getName(), 
                mobileBid, 
                isRotatingEvenly,
                sitelinkData[0],
                sitelinkData[1],
                sitelinkData[2],
                isUsingMobileAds,
                callExtData[0],
                callExtData[1],
                warnAboutBudgetAndSchedule
            ]);
        }
    }
}

我不知道为什么它不起作用,考虑到我在开始时声明阵列。值得注意的是,我确实将STRATEGISTS声明为多维数组,如下所示:

var STRATEGISTS = [
    ['346-963-8912','Brizza','Arcadio'],
    ['885-612-1069','Doria','Arcadio'],
    ['922-596-2785','Edgar','Arcadio']
]

有什么想法吗?

修改

根据要求我添加了:

Logger.log(STRATEGISTS.length);
Logger.log(STRATEGISTS);

第一个记录了51,第二个按预期记录了变量。

谢谢,

1 个答案:

答案 0 :(得分:0)

感谢Guffa,我意识到即使我把推送声明分成不同的行,它也被视为一个。我原以为问题出在我的.push实现上,而是推送其中一个变量的问题。

事实证明我错过了回归

var callExtData = checkCallExtensions(campaign);

所以callExtData为空,因此未定义。

谢谢!