微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

类型错误:无法获取 mern

如何解决类型错误:无法获取 mern

我正在学习“React、NodeJS、Express 和 MongoDB - MERN 全栈指南”部分“将 React.js 前端连接到后端”。任何人都可以指导我为什么会收到此错误?每当我更新或删除一个地方时,它都会显示错误:TypeError: Failed to fetch I can do about that?

enter image description here

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

解决方法

找到答案:那是内核错误。你只需要添加 cors。

var cors = require('cors');

app.use(cors(),(req,res,next) = {}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。