# Uncaught TypeError when using --optimize

**URL:** <https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757>\
**Category:** Request Feedback\
**Created:** [June 6, 2019, 9:35am UTC](https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757 "2019-06-06T09:35:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasperV](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@JasperV](https://discourse.elm-lang.org/u/JasperV)\
**Post date:** [June 6, 2019, 9:35am UTC](https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757/1 "2019-06-06T09:35:16Z")

</div>

Hi All,

I’m in the finishing state of our webapp development and added the --optimize flag to elm make.  
This resulted in following error:

```
Uncaught TypeError: Jr is not a function
```

I did not have this error before using the optimize flag.

By evaluating the code in Chrome dev tools I found out it is related to _Browser.Dom.getElement_.

Fyi: we have several rotary like sliders on some of the pages and we need the location of the slider to translate the touch coordinates in slider values.

My code:

```
getDomLocation : String -> Cmd Msg
getDomLocation taskId =
  ( Task.attempt
      (NewDomLocation taskId)
      (Browser.Dom.getElement taskId)
  )

getDomLocationVolumeSliders : Model -> ZoneId -> Cmd Msg
getDomLocationVolumeSliders model zoneId =
  let
    zone : Zone
    zone =
      case zoneId of
        Zone1 -> model.zone1
        Zone2 -> model.zone2
  in
    Cmd.batch
      [ getDomLocation zone.micAVolume.rotaryId
      , getDomLocation zone.micBVolume.rotaryId
      , getDomLocation zone.sourceVolume.rotaryId
      ]

init : Flags -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
  ( { initialModel | layout = updateLayout flags
                   , webSocket = WebSocket.updateUrl url initialModel.webSocket
    }
  , Cmd.batch
      [ (getDomLocationVolumeSliders initialModel Zone1)
      , ( setBodyBackgroundColor
            (updateLayout flags).device
            initialModel.theme
            initialModel.activePanel
        )
      ]
  )
```

If I replace the Cmd.batch in getDomLocationVolumeSliders with Cmd.none the error is gone. The function getDomLocationVolumeSliders is called in several places, but as the error appeared on load, I first checked the init function. I replaced Cmd.none again with the Cmd.batch and commented the getDomLocationVolumeSliders call in the init function. The error was gone.

If I called getDomLocationVolumeSliders from other places it just worked.

I solved the error by not giving initalModel to getDomLocationVolumeSliders but the updated initialModel.

My working code: (with --optimize flag)

```
init : Flags -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
  let
    initialModel : Model
    initialModel =
      { emptyModel | layout = updateLayout flags
                   , webSocket = WebSocket.updateUrl url emptyModel.webSocket
      }
  in
    ( initialModel
    , Cmd.batch
        [ (getDomLocationVolumeSliders initialModel Zone1)
        , ( setBodyBackgroundColor
              (updateLayout flags).device
              initialModel.theme
              initialModel.activePanel
          )
        ]
    )
```

I did not change function getDomLocation or function getDomLocationVolumeSliders.  
So by giving getDomLocationVolumeSliders the updated intial model the error is solved. While it only takes 3 strings from the model and those strings do not change.

Strange no? Isn’t this a runtime error? I did not have any Elm compile errors.

I solved it for my implementation, so I’m happy 🙂. I just wanted to discuss what I possibly did wrong or maybe it’s something wrong with the optimization steps that are applied with --optimize.

Thanks!  
Jasper

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [June 6, 2019, 10:43am UTC](https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757/2 "2019-06-06T10:43:11Z")

</div>

Maybe this known issue: [https://github.com/elm/compiler/issues/1836](https://github.com/elm/compiler/issues/1836) ?

---

<div class="post-metadata">

**Author:** ![JasperV](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@JasperV](https://discourse.elm-lang.org/u/JasperV)\
**Post date:** [June 6, 2019, 1:30pm UTC](https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757/3 "2019-06-06T13:30:08Z")

</div>

It looks like it’s related but then why is the issue solved when I restructure the init function?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [June 16, 2019, 1:30pm UTC](https://discourse.elm-lang.org/t/uncaught-typeerror-when-using-optimize/3757/4 "2019-06-16T13:30:13Z")

</div>

Closed on June 16, 2019, 1:30pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
