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>
和我的搜索xml代码:
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);
您正在创建一个"外部" 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
,则将停止)。
您在该回调函数内重新声明result
可能是问题所在,尽管我不确定.find()
方法的作用。如果它是异步的,那么整个代码无论如何都不能工作。
去掉这里的var
:
var result= {
使得成为另一个变量,一个在回调中。
相关文章:
- 如何使用Phonegap将图像对象保存到Android中的文件中
- 如何将对象保存到谷歌UI实例中
- 将表单对象保存到具有主干.js的模型
- 如何将非常大的内存中对象保存到文件
- 将对象保存在数组中以获取高分列表
- 为什么不可能将对象保存在二维数组中
- 使用php脚本将javascript对象保存到数据库中
- 将对象保存到angularjs中的JSON文件中
- 对象保存时未调用分析回调
- KineticJS:保存到JSON:对象保存了多次
- 如何将json对象保存为文件服务器端
- 将Javascript对象保存到NoSQL数据库中,然后检索它
- 通过NodeJS将对象保存到DynamoDB实例
- 将画布对象保存为图像时出现安全错误
- 将DOM对象保存在内存中Vs将HTML字符串保存在内存中以便重复使用
- 我如何将对象保存到Node.js中的文件
- 如何将对象保存为json字符串到数据库并稍后重新加载
- 将Javascript文件对象保存到本地存储
- 在AngularJS中将对象保存到控制器中的数组中
- 字符编码- Javascript Blob对象保存为文件包含额外的字节