繁体   English   中英

Javascript-尝试将函数的结果保存到对象。 对象为空

[英]Javascript - Trying to save the result of a function to an object. Object is null

在一个名为searchXML的单独函数中,传递了一些参数,并且我正在解析一个xml文件,以基于这些参数返回一些特定值,并将这些值保存到对象中。 然后,在另一个函数中,调用searchXML()并将结果保存到一个对象。 不幸的是,那个对象原来是空的。 我对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);

尽管我不确定.find()方法的作用,但您在该回调函数中重新声明result可能是问题.find() 如果它是异步的,那么总的代码将无法正常工作。

在这里拿走var

       var result= {

这将产生另一个变量,即回调中的一个。

您正在创建“外部” 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 ,则将停止)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM