redux-logic icon indicating copy to clipboard operation
redux-logic copied to clipboard

REDUX-LOGIC (TypeError: Cannot read property 'toString' of undefined.)

Open robertqjohnson opened this issue 7 years ago • 8 comments

Hello,

I’m using redux-logic 0.15.0 and I’m getting the following error: • TypeError: Cannot read property 'toString' of undefined.

I debugged the createlogic and createlogicmiddleware JavaScript files and found out that the following createlogicmiddleware function is returning type undefined to the stringifyType function in utils.js. The field Tranform has a value of undefined if it’s not used. Below is my redux logic. Let me know if you need anything else or have come across this issue before.

Createlogicmiddleware: function naming(logic, idx) { if (logic.name) { return logic; } return _extends({}, logic, { name: 'L(' + stringifyType(logic.type) + ')-' + idx }); }

My Logic: import { createLogic } from 'redux-logic'; import * as appConfig from '../../appConfig'; import actionTypes from '../actions'; import { actions } from '../actions'; import { IOption } from '../../models/IOption'; import { IEnvironmentDropDownState } from '../../store/state/IEnvironmentDropDownState';

const getEnvironmentOptionsLogic = createLogic({ cancelType: actionTypes.GET_ENVIRONMENT_OPTIONS_CANCEL, latest: true, name: 'GetEnvironmentOptions', processOptions: { dispatchReturn: true, failType: actions.getEnvironmentOptionsFail, successType: actions.getEnvironmentOptionsSuccess, }, type: actionTypes.GET_ENVIRONMENT_OPTIONS_INPROGRESS, validate({ getState, action }, allow, reject) { const state: IEnvironmentDropDownState = getState(); if (!state.options.length) { allow(action); } else { reject(); } },

async process({ httpClient }) {
  return await httpClient.get(appConfig.GetEnvironmentsURL).then((response: IOption[]) => response);
},

});

export default [ getEnvironmentOptionsLogic, ];

Robert Johnson IT Engineer

robertqjohnson avatar Jun 30 '18 01:06 robertqjohnson

I had this problem as well and the problem was that I exported an array of logics instead of the logic itself. I see you did this as well, so depending on how you apply this logic in the middleware, you might have the same issue:

export default [
  getEnvironmentOptionsLogic,
];

should be:

export default getEnvironmentOptionsLogic;

kschiffer avatar Jul 03 '18 12:07 kschiffer

Kevin, I just tried your solution and it fixed my typescript error. THANK YOU VERY MUCH for the quick response and resolution. I have been trying to resolve this for the past three days and I really appreciate your feedback. Have a happy and safe 4th of July.

robertqjohnson avatar Jul 03 '18 15:07 robertqjohnson

@robertqjohnson: while this is a nice sentiment, I doubt if @kschiffer celebrates American Independence, since I believe he is German living in Amsterdam :-)

SideBar: @kschiffer, lest you think I am a stalker, I was researching your website. Very Nice! I am unfamiliar with .tpl template files ... Is this part of NunJuncks? Have you ever researched the react static site generator called Gatsby? Just curious.

KevinAst avatar Jul 03 '18 16:07 KevinAst

@KevinAst Cool that you like my website :). Indeed these are nunjuck files – I know gatsby but I think it can be a little overkill for small sites. I like to be in full control, especially for small websites so I decided to more or less go from scratch and use a simple templating engine that just fit my basic needs. As far as Static Site Generators go, I can also recommend Hugo a lot.

kschiffer avatar Jul 04 '18 09:07 kschiffer

Before closing this issue, it seems like there is an opportunity to improve redux-logic.

If I understand the issue, createLogicMiddleware() is erroring out in a very obscure way, while interpreting in invalid client input parameter. If this is correct, it seems like the library should perform better type checking, emitting a more appropriate error ... say this:

***ERROR*** createLogicMiddleware(): unexpected input ... expecting Logic[]

Instead of this:

TypeError: Cannot read property 'toString' of undefined.

What say you: @Jeffbski, @robertqjohnson, @kschiffer

KevinAst avatar Jul 05 '18 11:07 KevinAst

Definitely agree. I had to dig quite a bit into my debugger to understand the issue. A more helpful error message would make troubleshooting a lot easier.

kschiffer avatar Jul 05 '18 21:07 kschiffer

Thanks for bringing this up. Yes, we can certainly improve the validation in createLogicMiddleware which would hopefully have helped catch this error sooner.

jeffbski avatar Jul 09 '18 21:07 jeffbski

I'll reopen so I remember to add more validation.

jeffbski avatar Jul 09 '18 21:07 jeffbski