2016-11-16 86 views
2

我在elm中收到未捕获的typeerror,不知道为什么。未捕获TypeError:无法读取undefined属性“星期”

我从api解码json字符串; api列出rostars,每个rostar都有或者 a planningId或者flexplanningId。我想要在列表中映射,并根据planningIdflexplanningId根据其具体情况为每个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

感谢帮帮我!

回答

0

这可能不是你的错误, 的来源,但你的员工解码器说,它需要一个rostar变量,它包含一个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

我觉得你的问题是由letupdateResponse映射函数employee结合造成的。标签employee已经存在,所以这一行导致递归定义。

let 
    employee = 
     { employee | week = mapWeek employee.week } 

在榆树0.18,这是一个编译错误,并给你一个详细的错误信息,而不是离开的可能性为一个运行时错误:

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中,他们为一些最基本的问题添加了编译器检查。

相关问题