如何解决MSW随机404误差与React

发布于 2025-01-27 13:55:20 字数 1826 浏览 3 评论 0原文

我已经开始使用MSW嘲笑BE,但是当我保存更改时,它随机地无法获取数据。不要真正知道如何调试,因为它是非常随机的

API路由功能

import Api from './Api';

export default class BrandsAPIs extends Api {
  getAllBrands(accessToken) {
    return this.instance.get('brands', this.constructor.addAuthorizationHeader(accessToken));
  }
}

browser.js

import { setupWorker, rest } from 'msw';
import brands from './getBrands.json';

const worker = setupWorker(
  rest.post('/authentication', (req, res, ctx) => res(ctx.json({
    accessToken: 'Freebee',
    user: 'Andrea',
  }))),
  rest.get('/brands', (req, res, ctx) => res(ctx.json(brands))),
);

worker.start();

react页面

import React, { useState, useEffect } from 'react';
import { BrandsAPIsSingleton } from '../../modules/api';
// components
import BrandsMainTable from './table/BrandsMainTable';

export default function Main() {
  const [isLoading, setIsLoading] = useState([]);
  const [brands, setBrands] = useState([]);

  useEffect(() => {
    setIsLoading(true);
    BrandsAPIsSingleton.getAllBrands()
      .then((r) => {
        setBrands(r.data);
        setIsLoading(false);
      }).catch((error) => {
        setIsLoading(false);
        console.log(error);
      });
  }, []);

  if (isLoading) {
    return <div>Loading...</div>;
  }
  return (
    <BrandsMainTable data={brands} />
  );
}

其他人都有这个问题?

I have started using MSW to mock the BE, but randomly it fails to fetch data when I save changes. Dont' really know how to debug it, since it's very random
enter image description here
enter image description here

Api routing functions

import Api from './Api';

export default class BrandsAPIs extends Api {
  getAllBrands(accessToken) {
    return this.instance.get('brands', this.constructor.addAuthorizationHeader(accessToken));
  }
}

browser.js

import { setupWorker, rest } from 'msw';
import brands from './getBrands.json';

const worker = setupWorker(
  rest.post('/authentication', (req, res, ctx) => res(ctx.json({
    accessToken: 'Freebee',
    user: 'Andrea',
  }))),
  rest.get('/brands', (req, res, ctx) => res(ctx.json(brands))),
);

worker.start();

react page

import React, { useState, useEffect } from 'react';
import { BrandsAPIsSingleton } from '../../modules/api';
// components
import BrandsMainTable from './table/BrandsMainTable';

export default function Main() {
  const [isLoading, setIsLoading] = useState([]);
  const [brands, setBrands] = useState([]);

  useEffect(() => {
    setIsLoading(true);
    BrandsAPIsSingleton.getAllBrands()
      .then((r) => {
        setBrands(r.data);
        setIsLoading(false);
      }).catch((error) => {
        setIsLoading(false);
        console.log(error);
      });
  }, []);

  if (isLoading) {
    return <div>Loading...</div>;
  }
  return (
    <BrandsMainTable data={brands} />
  );
}

Anybody else had this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文