简体   繁体   English

ReactJS Redux:未捕获的类型错误:无法读取未定义的属性(读取“类型”)

[英]ReactJS Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'type')

Inside MainComponent class on DidMount I am running the following:在 DidMount 上的 MainComponent class 内部,我正在运行以下命令:

  getResultsSearchURL = () => {
    if (this.props.tokenized && this.props.match.params.searchQuery) {
      console.log("tokenized");

      this.props.dispatch(
        getResult({
          key: this.props.match.params.searchQuery,
        })
      );
    }
  };

  componentDidMount() {
    console.log("mounted");
    this.getResultsSearchURL();
  }

And in Redux currently just logging to console:而在 Redux 目前只是登录到控制台:

export const getResult = (item) => console.log({ item });

What is causing these errors?是什么导致了这些错误? Am I doing something wrong here?我在这里做错了吗?


Getting the following errors:收到以下错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'type')
    at reducer (createReducer.ts:233)
    at reducer (createSlice.ts:325)
    at MainContainer.getResultsSearchURL (MainContainer.jsx:43)
    at MainContainer.componentDidMount (MainContainer.jsx:53)
    at commitLifeCycles (react-dom.development.js:20663)
    at commitLayoutEffects (react-dom.development.js:23426)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at commitRootImpl (react-dom.development.js:23151)

react-dom.development.js:20085 The above error occurred in the <MainContainer> component:

    at MainContainer (http://localhost:3000/static/js/bundle.js:7157:5)
    at ConnectFunction (http://localhost:3000/static/js/bundle.js:54017:68)
    at Route (http://localhost:3000/static/js/bundle.js:57231:29)
    at Switch (http://localhost:3000/static/js/bundle.js:57433:29)
    at Portal (http://localhost:3000/static/js/bundle.js:7523:5)
    at ConnectFunction (http://localhost:3000/static/js/bundle.js:54017:68)
    at Router (http://localhost:3000/static/js/bundle.js:56862:30)
    at BrowserRouter (http://localhost:3000/static/js/bundle.js:56484:35)
    at Fe (http://localhost:3000/static/js/bundle.js:63987:60)
    at ye (http://localhost:3000/static/js/bundle.js:63821:61)
    at Provider (http://localhost:3000/static/js/bundle.js:53729:20)
    at Partner

createReducer.ts:233 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type')
    at reducer (createReducer.ts:233)
    at reducer (createSlice.ts:325)
    at MainContainer.getResultsSearchURL (MainContainer.jsx:43)
    at MainContainer.componentDidMount (MainContainer.jsx:53)
    at commitLifeCycles (react-dom.development.js:20663)
    at commitLayoutEffects (react-dom.development.js:23426)
    at HTMLUnknownElement.callCallback (react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:3994)
    at invokeGuardedCallback (react-dom.development.js:4056)
    at commitRootImpl (react-dom.development.js:23151)

Reducer store:减速机店:

const reducer = combineReducers({
  authenticator,
  alert,
  search,
  body,
});
const store = configureStore({
  reducer,
  middleware: [...getDefaultMiddleware(), api],
});

export default store;

Slice:片:

const slice = createSlice({
  name: "u/Body",
  initialState: {
    loading: false,
    search: {
      list: [],
      listFiltered: [],
      availableStock: false,
      supplierStock: {
        code: [],
        list: [],
        loading: false,
        updated: true,
      },
    },
  },
  reducers: {
    searchStarted: (body, action) => {
      body.loading = true;
      body.search.listFiltered = [];
      body.search.supplierStock.code = [];
      body.search.supplierStock.list = [];
      const filterKeys = Object.keys(body.search.filter);
      filterKeys.forEach((k) => {
        body.search.filter[k].list = [];
      });
    },
    searchRecieved: (body, action) => {
      body.search.list = action.data.l;
      body.search.supplierStock.code = action.data.c;
      body.loading = false;
      const filterKeys = Object.keys(body.search.filter);
      //...
    },
   //...
  },
});


export const { filterUpdated } = slice.actions;
export default slice.reducer;

export const getResult = (item) =>
  partnerApiStart({
    url: "/search/result",
    method: "post",
    authenthicateRequired: true,
    onStart: slice.actions.searchStarted.type,
    onSuccess: slice.actions.searchRecieved.type,
    //onError: slice.actions.searchFailed.type,
    data: { k: item.key },
  });

API Middleware: API 中间件:

export const partnerApiStart = createAction("UNI/API/started");

const api = (store) => (next) => async (action) => {
  console.log(action);

  if (action.type !== partnerApiStart.type) {
    return next(action);
  }
  next(action);
//...

I think (at least one of) the problem is that partnerApiStart is an action creator, which is a function, not an action.我认为(至少一个)问题在于partnerApiStart是一个动作创建者,它是一个 function,而不是一个动作。 So partnerApiStart.type should be undefined in here action.type.== partnerApiStart.type .所以partnerApiStart.type应该在这里undefined action.type.== partnerApiStart.type I suspect this is related to the error you are seeing but doesn't directly explain the null pointer.我怀疑这与您看到的错误有关,但没有直接解释 null 指针。

Reference: documentation for createAction https://redux-toolkit.js.org/api/createAction参考: createAction https://redux-toolkit.js.org/api/createAction的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type') redux reactjs - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'type') redux reactjs Redux 工具包:未捕获的类型错误:无法读取未定义的属性(读取“类型”) - Redux toolkit: Uncaught TypeError: Cannot read properties of undefined (reading 'type') 反应,Redux:未捕获的类型错误:无法读取未定义的属性(读取“区域”) - React, Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'region') ReactJs:未捕获的类型错误:无法读取未定义的属性(读取“0”) - ReactJs: Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“名称”)-ReactJS - Uncaught TypeError: Cannot read properties of undefined (reading 'name') - ReactJS 未捕获的类型错误:无法读取未定义的属性(读取“类型”) - Uncaught TypeError: Cannot read properties of undefined (reading 'type') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“0”) - Uncaught TypeError: Cannot read properties of undefined (reading '0') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') 未捕获的类型错误:无法读取未定义的属性(读取“”) - Uncaught TypeError: Cannot read properties of undefined (reading '')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM