【发布时间】:2022-02-01 20:46:55
【问题描述】:
所以我在 heroku 上托管了我的网站。一切都很顺利并且工作正常,直到我进入使用 fetch 功能的页面。 在控制台(生产)中它说
Failed to load resource: net::ERR_CONNECTION_REFUSED
QuestionsList.js:9 Uncaught (in promise) TypeError: Failed to fetch
at QuestionsList.js:9:19
at s (runtime.js:63:40)
at Generator._invoke (runtime.js:294:22)
at Generator.next (runtime.js:119:21)
at r (asyncToGenerator.js:3:20)
at i (asyncToGenerator.js:25:9)
at asyncToGenerator.js:32:7
at new Promise (<anonymous>)
at asyncToGenerator.js:21:12
at QuestionsList.js:8:18
我的代码问题列表组件是
const QuestionsList = () => {
const [data, setdata] = useState([]);
const fetchData = async () => {
var x = await fetch("http://localhost:4000/api/v1/post");
var parsedData = await x.json();
setdata(parsedData.posts);
};
useEffect(() => {
fetchData();
}, []);
return (
<>
<div className="container">
<h2 className="heading-mid currentAffairs mt-3 pt-3">
Current Affairs!!{" "}
</h2>
<div className="row">
{data.map((element) => {
return (
<div className="col md-4" key={element.imageUrl}>
<Question url={element.imageUrl} />
</div>
);
})}
</div>
</div>
</>
);
};
我认为问题出在 var x = await fetch("http://localhost:4000/api/v1/post"); 但无法弄清楚要写什么而不是localhost(我知道我必须写我的域名)。就像我会在一段时间后更改域。所以除了硬编码还有其他方法吗? 提前致谢。
【问题讨论】:
标签: javascript reactjs fetch localhost