提问人:Tanvesh 提问时间:11/2/2022 更新时间:11/3/2022 访问量:787
有一个错误,TypeError:无法使用 react-xml-parser 读取未定义的属性(读取“replace”)
There is an error , TypeError: Cannot read properties of undefined (reading 'replace') using react-xml-parser
问:
我正在尝试将XML文件获取到我的react组件中,但由于以下错误而无法获取,我被卡住了很长时间,请帮助我解决这个问题。
链接到 XML:https://7targets.ai/blog/feed.xml
这是我名为BlogFeed.js的组件。
import React, { useEffect, useState, Component } from "react";
import axios from "axios";
import ReactDOM from "react-dom";
import XMLParser from 'react-xml-parser';
import { parse } from 'fast-xml-parser';
// import "react-responsive-carousel/lib/styles/carousel.min.css"; // requires a loader
// import { Carousel } from "react-responsive-carousel";
import "./sample.scss";
const BlogFeed = () => {
const [data , setData] = useState();
useEffect(() => {
axios
.get("https://7targets.ai/blog/feed.xml", {
"Content-Type": "application/xml; charset=utf-8",
})
.then((response) => {
setData(response.data)
var xml = new XMLParser().parseFromString(data); // Assume xmlText contains the example XML
console.log(xml);
}).catch((error)=>{
console.log("There is an error , ",error)
});
}, []);
console.log(data);
return (
<>
</>
);
};
export default BlogFeed;
当我使用 XMLparser 时,它向我抛出一个错误,说:
There is an error , TypeError: Cannot read properties of undefined (reading 'replace')
at e.value (bundle.js:1:1)
at e.value (bundle.js:1:1)
at BlogFeed.js:19:1
请帮助我度过难关。
答:
0赞
vinayr
11/3/2022
#1
setData
是异步函数,因此当您将其传递给 XML 解析器时仍然如此。只需将 axios 响应数据直接传递给 xml 解析器即可。data
undefined
setData(response.data)
var xml = new XMLParser().parseFromString(response.data);
评论