0

i have written following code .. this is my event action to store the events.

in eventAction.js

export const storeAndUpdateEvent = (events) => dispatch => {
dispatch(setLoading())
d.ref(`/events`).push(events).then(
    res => {
        if (res.key) {
            dispatch(addEvent(events))
            dispatch(closeModal())
        }
        dispatch(closeLoading())


    }
)

}

in eventReducer :

        case actionType.ADD_EVENT:
        console.log(state.events)
        return {
            ...state,
            events: [this.state.events,actions.eventPayload],
            isLoading:false
        };

finally in event.js

 {
            !isLoading ?
                events ?
                Object.values(events).map((event, index) => {
                    return (
                        <EventList event={event} key={index} />
                    )
                }) : <p>You Have'n involved in any events</p> : 'Loading .....'
        }

the error is

  • What error do you get? – Adam Jeliński Apr 09 '20 at 18:07
  • Unhandled Rejection (TypeError): state.events is not iterable push../src/redux/reducers/eventReducer.js.__webpack_exports__.default src/redux/reducers/eventReducer.js:35 32 | console.log(state.events) 33 | return { 34 | ...state, > 35 | events: [...state.events, actions.eventPayload], | ^ 36 | isLoading:false 37 | }; 38 | case actionType.UPDATE_EVENT: – Rajeev Rajchal Apr 10 '20 at 01:49
  • It looks like events is an object at some point rather than an array, so you are trying to change the type of it in your eventReducer which will cause issues somewhere even when it does run because JS is dynamically typed. – Zachary Haber Apr 10 '20 at 03:34

1 Answers1

0

You have a mistake in the eventReducer. You are outputting an array, instead of an object.

    case actionType.ADD_EVENT:
    console.log(state.events)
    return {
        ...state,
        events: {
            ...this.state.events,
            [actions.eventPayload.type]: actions.eventPayload.value   //guessed the properties of the payload
        ],
        isLoading:false
    };

Change the eventPayload.type and actions.eventPayload.value to whatever you have in your actual data.

Adam Jeliński
  • 1,708
  • 1
  • 10
  • 21