redux-logic
redux-logic copied to clipboard
REDUX-LOGIC (TypeError: Cannot read property 'toString' of undefined.)
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
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;
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: 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 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.
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
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.
Thanks for bringing this up. Yes, we can certainly improve the validation in createLogicMiddleware which would hopefully have helped catch this error sooner.
I'll reopen so I remember to add more validation.