1

I receive the following error after performing an HTTP post:

Unable to get property 'tag' of undefined or null reference

I believe the error occurs when executing the following decoder function:

sourceDecoder : Decoder JsonSource
sourceDecoder =
    Decode.map5 JsonSource
        ...
        (field "Links" providerLinksDecoder)

Decoder Dependencies:

providerLinksDecoder : Decoder JsonProviderLinks
providerLinksDecoder =
    Decode.map JsonLinkFields
        (field "Links" <| Decode.list (Decode.lazy (\_ -> linkDecoder)))
        |> Decode.map JsonProviderLinks

linkDecoder : Decoder JsonLink
linkDecoder =
    Decode.map6 JsonLink
        (field "Profile" profileDecoder)
        ...

profileDecoder : Decoder JsonProfile
profileDecoder =
    Decode.map7 JsonProfile
        ...
        (field "Sources" <| Decode.list (Decode.lazy (\_ -> sourceDecoder)))

Appendix:

type JsonProviderLinks
    = JsonProviderLinks JsonLinkFields


type alias JsonLinkFields =
    { links : List JsonLink
    }

The source code can be found on here.

Note: I attempted to research this error and came across this page. As a result, I attempted to use the Decode.lazy function. However, my attempt failed.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118

1 Answers1

2

There's a lot of decoders that rely on other decoders in your examples. You've changed some of them to use Decode.lazy, but not all, and that error you've received will happen when there's some out of control recursion.

You don't need a list to be able to use lazy. Try - as a first step towards debugging, at least - to change all decoders that reference other decoders to use Decode.lazy. For example:

sourceDecoder : Decoder JsonSource
sourceDecoder =
    Decode.map5 JsonSource
        ...
        (field "Links" (Decode.lazy (\_ -> providerLinksDecoder)))
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
  • See also https://github.com/elm-lang/elm-compiler/issues/873 and https://github.com/elm-lang/elm-compiler/blob/master/hints/bad-recursion.md – Matt McHenry Oct 21 '17 at 01:46