我正在尝试在Javascript中执行一些非常基本的操作,并通过xml文件读取并将数据推送到数组中。从我所做的所有搜索中,语法只是this.xxx.push(itemTobePushed)。
然而,我无法阅读财产' payrules'未定义的。
它是一个带有打字稿
的angular5项目CODE
payrules = []; // Create empty array
testXml(inputValue: any) {
const file: File = inputValue.files[0];
const myReader: FileReader = new FileReader();
myReader.onloadend = function (e) {
xmlLoader.parseString(myReader.result, function (err, response) {
response.Kronos_WFC.Response.forEach(payrule => {
payrule.WSAPayRule.forEach(name => {
console.log('Pay Rule Name: ', name.$.Name); // This works fine and outputs a lits of payrules
this.payrules.push(name.$.Name); // This fails with Cannot read property 'payrules' of undefined
});
});
});
};
myReader.readAsText(file);
}