mschipperheyn / normalizr-immutable

Other
123 stars 13 forks source link

Cannot read property 'entities' of undefined #18

Closed srghma closed 7 years ago

srghma commented 7 years ago

projects.toString() // from "state.projects", Map instance and many other functions throw this error (eg ownProps.project.tasks.includes(taskId) there)

I saw you redefined many functions from immutable, maybe this is the source

mschipperheyn commented 7 years ago

Can you provide some more code for me to understand. And which version are you using?

srghma commented 7 years ago

Sorry, I move from normalizr-immutable to vanilla Immutable.fromJS for better handleability. But there is commit of this issue

I think origin of this issue is that initialState of project reducer is Map({}) there

import { Map } from "immutable"
import * as types from "../projectConstants"

const initialState = Map({})

export default function projects(state = initialState, action = {}) {
  switch (action.type) {
  case types.LOAD_PROJECTS: {
    return state.merge(action.payload)
  }
  default:
    return state
  }
}

And then I merge it like this dispatch(loadProjects(normalized.entities.projects))

And when I try use it state.projects.toString(), it throw Cannot read property 'entities' of undefined error

mschipperheyn commented 7 years ago

Ok, at a glance, it seems to me that you should merge entities and not entities.projects.

Also, "property 'entitities' of undefined" suggests that the projectsReducer is not available when that call is made. This could be related to not supplying the reducer reference correctly in the schema definition.

My personal preference is to make the state a Record instead of a Map. I think it's more elegant since it kind of defines an API.

I would define it for instance as

 const ProjectState = Record({
    entities: Map(),
    result: OrderedSet()
})

const initialState = new ProjectState();
srghma commented 7 years ago

Yes, or I had to use NormalizedRecord as initialState, I just surprised that entities created by "normalizr-immutable" is not compatible with vanilla immutable entities at first glance, I will close the issue, thanks you for your time

P.S. I thought that Record doesn`t have Iterable methods, I was misguided by official documentation :smile:

mschipperheyn commented 7 years ago

The entities created by normalizr-immutable are simple immutable Records, fully compatible. If you start using the Proxy facilities, you have to take a little more care in how you use things. But for me, the pains are worth it.

One more thing: I recommed using the latest 0.0.4 beta of normalize-immutable. Depending on configuration the 0.0.3 branch uses a Record for the entities structure which was a misguided choice on my part.

srghma commented 7 years ago

Ok, currently I just teaching react, it seems I will return to normalizr-immutable once)

srghma commented 7 years ago

OK, I returned to normalizr-immutable, and find out that Records dont have useful Iterable methods (map/filter, just as I say, actually they have them, but they return undefined and will show warnings in future versions, spent plenty of time on that), so I better use Maps And because there more cozy way to immutate initial state with vanilla fromJS(is that so?), I move to that