gpt4 book ai didi

javascript - Node : TypeError: callback is not a function

转载 作者:搜寻专家 更新时间:2023-10-31 23:29:56 25 4
gpt4 key购买 nike

我写了下面的代码来读取一个 xml 并返回一个 hashmap:

this.xmlObjectRepositoryLoader = function (xmlPath, callback){
var map = {}
var innerMap = {};
var el;
fs.readFile(xmlPath, "utf-8",function(err, data) {
if(err){
console.log('File not found!!')
}
else{
console.log(data)
var doc = domparser.parseFromString(data,"text/xml");
var els = doc.getElementsByTagName("Child");
for(var i =0 ; i< els .length;i++){
var e = elements[i];
eName = e.getAttribute("a");
var params = elm.getElementsByTagName("abc");
innerMap = {};
for(var j =0 ; j< params.length;j++){
var param = params[j];
var b = param.getAttribute("b");
var c= param.getAttribute("c");
innerMap[b] = c;
}
map[el] = innerMap;
innerMap={};
};
}
console.log(map);
return callback(map);
});
};

我从下面的方法调用 xmlObjectRepositoryLoader 但它返回错误 TypeError: callback is not a function:

this.objectLoader = function(filePath){
if (filePath.includes(".xml")) {
console.log(this.xmlObjectRepositoryLoader(filePath));
}

你能告诉我我错在哪里吗,我该如何解决

最佳答案

你正在尝试调用 callback,在这里:

return callback(map);

但是,您没有将回调传递给 xmlObjectRepositoryLoader:

console.log(this.xmlObjectRepositoryLoader(filePath));

改为这样做:

this.xmlObjectRepositoryLoader(filePath, function(map){ 
console.log(map)
});

关于javascript - Node : TypeError: callback is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39406641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com