Search code examples
modelelmflags

Elm create-elm-app Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 ways


I am working on an elm app, and when I try to change the Model to anything other than {} I get the error

Error: Problem with the flags given to your Elm program on initialization. Json.Decode.oneOf failed in the following 2 ways: (1) Problem with the given value: undefined Expecting null (2) Problem with the given value: undefined Expecting an INT

The error is reproduced here


Solution

  • The error is because init is expecting Maybe Model but in HTML section of Ellie you’re not passing in a flags.

    There are two options, you can either handle the flags, or remove the flags.

    To keep parsing flags: You’ll want to change Elm.Main.init({ node: document.querySelector('main') }) to be

    var app = Elm.Main.init({
      node: document.querySelector('main'),
      flags: <some value>
    });
    

    It’s also usually recommended to change your flags to be Json.Decode.Value and manually decode them So your init would become

    init : Value -> ( Model, Cmd Msg )
    init flags =
      case Json.Decode.decodeValue flagDecoder flags of
        Ok decodedFlags -> ...
        Err err -> ...
    

    that way you can handle the flags being invalid or missing.

    Remove Flag parsing

    The common way is to us the unit type (). So your main becomes

    main : Program () Model Msg
    

    and

    init becomes

    init : () -> ( Model, Cmd Msg )