我在elm中收到未捕获的typeerror,不知道为什么。未捕获TypeError:无法读取undefined属性“星期”
我从api解码json字符串; api列出rostars
,每个rostar都有或者 a planningId
或者flexplanningId
。我想要在列表中映射,并根据planningId
或flexplanningId
根据其具体情况为每个planning
分配一个唯一的id
。下面的代码:
记录定义和解码器:
type alias Rostar =
{ employee : Employee }
type alias Employee =
{ week : Week
, name : String
, id : Int
, contractHours : Float
}
type alias Week =
{ monday : List Planning
, tuesday : List Planning
, wednesday : List Planning
, thursday : List Planning
, friday : List Planning
, saturday : List Planning
, sunday : List Planning
}
type alias Planning =
{ time : String
, planningId : Maybe Int
, groupId : Int
, groupName : String
, flex : Bool
, employeeTimeslotId : Maybe Int
, flexplanningId : Maybe Int
, employeeId : Maybe Int
, id : Maybe Int
}
responseDecoder : Decoder (List Rostar)
responseDecoder =
list rostarDecoder
rostarDecoder : Decoder Rostar
rostarDecoder =
decode Rostar
|> required "employee" employeeDecoder
employeeDecoder : Decoder Employee
employeeDecoder =
decode Employee
|> required "rostar" weekDecoder
|> required "name" string
|> required "id" int
|> required "contract_hours" float
weekDecoder : Decoder Week
weekDecoder =
decode Week
|> required "monday" (list planningDecoder)
|> required "tuesday" (list planningDecoder)
|> required "wednesday" (list planningDecoder)
|> required "thursday" (list planningDecoder)
|> required "friday" (list planningDecoder)
|> required "saturday" (list planningDecoder)
|> required "sunday" (list planningDecoder)
planningDecoder : Decoder Planning
planningDecoder =
decode Planning
|> required "time" string
|> optional "planning_id" (nullable int) Nothing
|> required "group_id" int
|> required "group_name" string
|> required "flex" bool
|> optional "employee_timeslot_id" (nullable int) Nothing
|> optional "flexplanning_id" (nullable int) Nothing
|> required "employee_id" (nullable int)
|> hardcoded Nothing
映射:
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
HandleFeedResponse response ->
let
assignPlanningId : Planning -> Planning
assignPlanningId planning =
case planning.planningId of
Just id ->
{ planning | id = Just (id + 10000000) }
Nothing ->
case planning.flexplanningId of
Just id ->
{ planning | id = Just (id + 90000000) }
Nothing ->
{ planning | id = Nothing }
planningWithId : List Planning -> List Planning
planningWithId day =
List.map assignPlanningId day
mapWeek : Week -> Week
mapWeek week =
{ week
| monday = planningWithId week.monday
, tuesday = planningWithId week.tuesday
, wednesday = planningWithId week.wednesday
, thursday = planningWithId week.thursday
, friday = planningWithId week.friday
, saturday = planningWithId week.saturday
, sunday = planningWithId week.sunday
}
updateResponse : List Rostar
updateResponse =
List.map
(\r ->
let
employee =
{ employee | week = mapWeek employee.week }
in
{ r | employee = employee }
)
response
check =
Debug.log "updatedResponse" updateResponse
in
{ model | rostar = updateResponse } ! []
这里是我得到的错误:
Uncaught TypeError: Cannot read property 'week' of undefined Blockquote
感谢帮帮我!