未捕获的TypeError:无法读取属性'周'未定义的

时间:2016-11-16 11:31:42

标签: elm

我在榆树中得到了一个未被捕获的类型错误,并且不知道为什么。

我从api解码json字符串; api给出了rostars的列表,每个rostar都 planningIdflexplanningId。我希望在列表上进行映射,并根据planningid为每个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

映射:

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

这是我得到的错误:

  

未捕获的TypeError:无法读取属性'周'未定义的   块引用

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我认为您的问题是由let映射函数中employee的{​​{1}}绑定引起的。标签updateResponse已经存在,因此该行导致递归定义。

employee

在Elm 0.18中,这是一个编译错误,它会为您提供详细的错误消息,而不是为运行时错误留下可能性:

  

在1个模块中检测到错误。

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

-- BAD RECURSION ------------------------------------------------------ Main.elm 直接根据自身定义,导致无限循环。

employee
     

也许你正试图改变一个变量?榆树没有变异,所以当我   请参阅132|> employee = 133| { employee | week = mapWeek employee.week } 定义的employee,我将其视为递归   定义。尝试给新值一个新名称!

     

也许你想要一个递归值?要定义employee我们需要知道什么   employee是,所以让我们扩展它。等等,但现在我们需要知道employee   是的,所以让我们扩展它......这将继续无限!

     

要真正了解正在发生的事情以及如何解决问题,请查看:   https://github.com/elm-lang/elm-compiler/blob/0.18.0/hints/bad-recursion.md

在0.18之前,我会看到这些类型的奇数"未定义"意外执行某种意外递归时的运行时错误。在0.18中,他们为一些最基本的问题添加了编译器检查。

答案 1 :(得分: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