2016-11-16 4 views
2

느릅 나무에 잡히지 않은 typeerror가 표시되고 이유를 모르겠습니다.잡히지 않은 TypeError : 정의되지 않은 'week'속성을 읽을 수 없습니다.

API에서 json 문자열을 디코딩하고 있습니다. API는 rostars의 목록을 제공하고 각 rostar는 planningId 또는 flexplanningId 중 하나가 있습니다. 목록에 매핑하여 각 planning에 고유 한 id을 부여하고 이에 따라 planningId 또는 flexplanningId 중 하나를 기반으로합니다.

레코드 정의 및 디코더 :

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 

매핑 다음에 대한

Uncaught TypeError: Cannot read property 'week' of undefined Blockquote

감사 : 여기

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 } ! [] 

내가지고있어 오류가있어 여기 코드는 도움!

답변

0

오류의 원인이 아닐 수 있지만 직원 디코더에 변수가 필요하며 직원 번호는 week이어야합니다. 이 올바른지? 또는 week이라고해야합니까?

employeeDecoder : Decoder Employee 
employeeDecoder = 
    decode Employee 
--  |> required "rostar" weekDecoder -- is this correct? 
     |> required "week" weekDecoder  -- what I would have expected 
     |> required "name" string 
     |> required "id" int 
     |> required "contract_hours" float 
1

내가 문제가 updateResponse 매핑 기능에 employee의 바인딩 let에 의해 발생 생각 : 여기

는 코드입니다. 레이블 employee이 이미 있으므로이 줄은 재귀 적 정의의 원인이됩니다.

느릅 나무 0.18에서
let 
    employee = 
     { employee | week = mapWeek employee.week } 

, 이것은 컴파일 오류이며, 런타임 오류의 가능성을 떠나는 것보다 당신에게 자세한 오류 메시지를 제공합니다 :

Detected errors in 1 module.

-- BAD RECURSION ------------------------------------------------------ Main.elm 

employee is defined directly in terms of itself, causing an infinite loop.

132|>        employee = 
133|          { employee | week = mapWeek employee.week } 

Maybe you are trying to mutate a variable? Elm does not have mutation, so when I see employee defined in terms of employee , I treat it as a recursive definition. Try giving the new value a new name!

Maybe you DO want a recursive value? To define employee we need to know what employee is, so let’s expand it. Wait, but now we need to know what employee is, so let’s expand it... This will keep going infinitely!

To really learn what is going on and how to fix it, check out: https://github.com/elm-lang/elm-compiler/blob/0.18.0/hints/bad-recursion.md

이전 0.18에, 나는 이러한 유형을 볼 것이다 실수로 일종의 의도하지 않은 재귀를 수행 할 때 홀수 "정의되지 않은"런타임 오류가 발생합니다. 0.18에서는 가장 기본적인 유형의 문제에 대한 컴파일러 검사를 추가했습니다.

관련 문제