2014-07-18 37 views
0

在一個名爲searchXML的分離函數中,其中傳入了一些參數,我正在解析xml文件以根據參數返回一些特定值並將這些值保存到對象中。然後,在另一個函數中,調用searchXML()並將結果保存到一個對象。不幸的是,這個對象變成空的。我相對較新的JavaScript,所以我不知道是否它的邏輯或語法導致錯誤。Javascript - 嘗試將函數的結果保存到對象。對象爲空

的XML的格式如下:

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
<Row> 
    <Tab>MF_Act</Tab> 
    <Category>Product Store Sessions </Category> 
    <_2013_01_31>1</_2013_01_31> 
    <_2013_02_28>2</_2013_02_28> 
    <_2013_03_31>3</_2013_03_31> 
    <_2013_04_30>4</_2013_04_30> 
    <_2013_05_31>5</_2013_05_31> 
    <_2013_06_30>6</_2013_06_30> 
    <_2013_07_31>7</_2013_07_31> 
    <_2013_08_31>8</_2013_08_31> 
    <_2013_09_30>9</_2013_09_30> 
    <_2013_10_31>10</_2013_10_31> 
    <_2013_11_30>11</_2013_11_30> 
    <_2013_12_31>12</_2013_12_31> 
    <FY_2013>x</FY_2013> 
    <_2014_01_31>1</_2014_01_31> 
    <_2014_02_28>2</_2014_02_28> 
    <_2014_03_31>3</_2014_03_31> 
    <_2014_04_30>4</_2014_04_30> 
    <_2014_05_31>5</_2014_05_31> 
    <_2014_06_30>6</_2014_06_30> 
    <_2014_07_31>7</_2014_07_31> 
    <_2014_08_31>8</_2014_08_31> 
    <_2014_09_30>9</_2014_09_30> 
    <_2014_10_31>10</_2014_10_31> 
    <_2014_11_30>11</_2014_11_30> 
    <_2014_12_31>12</_2014_12_31> 
    <FY_2014>y</FY_2014> 
</Row> 
<Row> 
    <Tab>MF_Act</Tab> 
    <Category>YTD</Category> 
    <_2013_01_31>1</_2013_01_31> 
    <_2013_02_28>2</_2013_02_28> 
    <_2013_03_31>3</_2013_03_31> 
    <_2013_04_30>4</_2013_04_30> 
    <_2013_05_31>5</_2013_05_31> 
    <_2013_06_30>6</_2013_06_30> 
    <_2013_07_31>7</_2013_07_31> 
    <_2013_08_31>8</_2013_08_31> 
    <_2013_09_30>9</_2013_09_30> 
    <_2013_10_31>10</_2013_10_31> 
    <_2013_11_30>11</_2013_11_30> 
    <_2013_12_31>12</_2013_12_31> 
    <FY_2013>r</FY_2013> 
    <_2014_01_31>1</_2014_01_31> 
    <_2014_02_28>2</_2014_02_28> 
    <_2014_03_31>3</_2014_03_31> 
    <_2014_04_30>4</_2014_04_30> 
    <_2014_05_31>5</_2014_05_31> 
    <_2014_06_30>6</_2014_06_30> 
    <_2014_07_31>7</_2014_07_31> 
    <_2014_08_31>8</_2014_08_31> 
    <_2014_09_30>9</_2014_09_30> 
    <_2014_10_31>10</_2014_10_31> 
    <_2014_11_30>11</_2014_11_30> 
    <_2014_12_31>12</_2014_12_31> 
    <FY_2014>t</FY_2014> 
</Row> 

</Root> 

而且我searchXML代碼:

function searchXML(xml, goalTab, goalCategory){ 
    console.log('in search xml'); 
    //get the current year in 4 digits (yyyy) 
    var year = new Date().getFullYear(); 

    //gets the string value of the 2 digit number of the previous completed month (ie: currMonth = April (04), prevMonth = March (03)) 
    var prevMonth = new Date().getMonth().toString(); 
    if(prevMonth.toString().length == 1){ 
     prevMonth = '0'+prevMonth.toString(); 
    } 

    //check for leap year 
    if(year % 4 == 0){ 
     var feb = 29; 
    }else{ 
     var feb = 28; 
    } 

    //make array for number of days per month in this year 
    var daysInMonths = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 


    //get the row offset for the particular month 
    var rowOffset_Month = (year - 2013)*12 + 2 + parseInt(prevMonth); 

    var result = null; 

    $(xml).find('Row').each(function(){ // for each Row in xml 
     console.log('-------new row------') 
     var row = this; 
     var boolTab = $(row).find('Tab').text() == goalTab; 
     var boolCategory = $(row).find('Category').text() == goalCategory; 
     console.log(boolCategory); 
     console.log(boolTab); 

     if (boolCategory && boolTab) { 
      console.log('found match'); 
      var result= { 
       //get the row that corresponds with the calculated row offset 
       //month : arr[rowOffset_Month][i], 
       month : $(row).find('_'+year+'_'+prevMonth+'_'+daysInMonths[parseInt(prevMonth) - 1]).text(), //-----------------check for errors here-------------------- 

       //get the last row, which corresponds with the 2014 ytd 
       // ytd : arr[arr[1].length - 1][i] 
       ytd : $(row).find('FY_' + year).text() //-----------------check for errors here-------------------- 
      }; //END result obj 
     };// END if 
    });// END jquery function 

    return result; 
}; 

然後在一個單獨的功能:

var ann_appStarts_plan = searchXML(xml, "MF_Act", "Product Store Sessions "); 
console.log('plan month: ' + ann_appStarts_plan.month); 
console.log('plan ytd: ' + ann_appStarts_plan.ytd); 

而且在Firebug的錯誤:

TypeError: ann_appStarts_plan is null 
    console.log('plan month: ' + ann_appStarts_plan.month); 
+0

你會建議我如何解決這個問題?有沒有一種同步的方式來使用JQuery來查找xml中的特定元素? – YazanLpizra

+2

find()調用沒有任何異步。這不是一個Ajax或其他網絡服務調用。 –

回答

1

你正在創建一個 「外」 result

var result = null; 

$(xml).find('Row').each(function(){ // for each Row in xml 
// ... 

然後分配到一個新的, 「內部」 result

if (boolCategory && boolTab) { 
     console.log('found match'); 
     var result= { 
     // .. 
     }; 

,然後將其丟棄,我們回到原文:

return result; 

在您的任務中丟失了var

if (boolCategory && boolTab) { 
    result= { 
    month : $(row).find('_'+year+'_'+prevMonth+'_'+daysInMonths[parseInt(prevMonth) - 1]).text(), 

    ytd : $(row).find('FY_' + year).text() 
    }; 

    return false; 
}; 

,並返回false現在退出each()循環,比賽已經發現(each()將調用內部函數一次,在它被稱爲在列表中的每個對象,但會停止,如果內部函數曾經返回false )。

+0

如果我返回false,那麼函數將返回的整體正確?或者它會跳出循環? – YazanLpizra

+0

不,它只是擺脫了循環。它從'each()'中的內部'function()'返回。外部'返回結果'仍然成立,但現在它將有一些有用的回報。 –

+0

它很有效。非常感謝你 :)。這是有道理的。實際上只是刪除var,而不添加'return false;'糾正了問題 – YazanLpizra

1

您在該回調函數中重新申報result可能是問題,儘管我不確定那個.find()方法是幹什麼的。如果它是異步的,那麼總體代碼將無法工作。

帶走這裏的var

 var result= { 

這使得另一個變量,一個回調裏面。