Search code examples
javascriptreactjsecmascript-6react-reduxjavascript-objects

TypeError: values.map is not a function


I am trying to map the object but in meantime it gives me the following error: TypeError: values.map is not a function. I am new to React development.

I am using this method in Redux method

Code

export function createEvent(values) {
  let modifyValue = values.map(newValue => {
    return {
      subject: newValue.id,
      taskType: newValue.tasktype,
      time: newValue.time,
      date: newValue.date,
      notes: newValue.notes,
      createdAt: newValue.createdAt,
      updatedAt: newValue.updatedAt,
      deletedAt: newValue.deletedAt,
      userId: newValue.userId,
      customerId: localStorage.getItem("customerValue"),
      start: newValue.start,
      end: newValue.end
    };
  });
  return dispatch => {
    axios
      .post("api/diary/create", modifyValue)

      .then(response => {
        return dispatch({ type: SET_DIARY_TASK_SUCCESS, data: response });
      })

      .catch((xhr, status, err) => {
        return dispatch({ type: SET_DIARY_TASK_FAILURE, data: xhr });
      });
  };
}

Solution

  • Array.prototype.map() is using to iterate through array and create new array. If values is a flat object access to it properties explicitly with:

    values.propertyName
    

    Your case:

    let modifyValue = {
          subject: values.id,
          taskType: values.tasktype,
          time: values.time,
          date: values.date,
          notes: values.notes,
          createdAt: values.createdAt,
          updatedAt: values.updatedAt,
          deletedAt: values.deletedAt,
          userId: values.userId,
          customerId: localStorage.getItem("customerValue"),
          start: values.start,
          end: values.end
        };