我的课程是"React,NodeJS,Express & MongoDB -- MERN全栈指南“部分,”连接React.js前端和后端“。有人能告诉我为什么会出现这个错误吗?每当我更新或删除一个位置时,它显示错误: TypeError: Failed fetch,我能对此做些什么?

const UserPlaces = () => {
const [loadedPlaces, setLoadedPlaces] = useState();
const { isLoading, error, sendRequest, clearError } = useHttpClient();
const userId = useParams().userId;
useEffect(() => {
const fetchPlaces = async () => {
try {
const responseData = await sendRequest(
`http://localhost:5000/api/places/user/${userId}`
);
setLoadedPlaces(responseData.places);
} catch (err) { }
};
fetchPlaces();
}, [sendRequest, userId]);
const placeDeletedHandler = deletedPlaceId => {
setLoadedPlaces(prevPlaces =>
prevPlaces.filter(place => place.id !== deletedPlaceId)
);
};
return (
<React.Fragment>
<ErrorModal error={error} onClear={clearError} />
{isLoading && (
<div className="center">
<LoadingSpinner />
</div>
)}
{!isLoading && loadedPlaces && <PlaceList items={loadedPlaces} onDeletePlace={placeDeletedHandler} />}
</React.Fragment>
);
};
export default UserPlaces;前端:https://github.com/sharjeelyunus/peek-mern后端:https://github.com/sharjeelyunus/peek-mern-api
发布于 2021-04-08 05:56:53
找到了答案:这是内核错误。您只需添加cors即可。
var cors = require('cors');
app.use(cors(), (req, res, next) = {}https://stackoverflow.com/questions/66835734
复制相似问题