Task.perform expects the third argument to be a different type

I am trying to adapt the Elm tutorial to my own small project, and I am having problems with the Json.Decoder that I supply.

My code is as follows:

type Msg = RetrieveComments | FetchSucceed String | FetchFail Http.Error update : Msg -> Model -> ( Model, Cmd Msg) update msg model = case msg of RetrieveComments -> (model, retrieveComments) FetchSucceed whatever -> (model, Cmd.none) FetchFail error -> (model, Cmd.none) retrieveComments : Cmd Msg retrieveComments = let url = "/ReactTutorial/comments.json" in Task.perform FetchFail FetchSucceed (Http.get commentsCollectionDecoder url) commentsCollectionDecoder : Decode.Decoder (List Comment.Model) commentsCollectionDecoder = Decode.list commentDecoder commentDecoder : Decode.Decoder Comment.Model commentDecoder = Decode.object2 Comment.Model ("author" := Decode.string) ("content" := Decode.string) 

The model is just a record with two fields author and content .

The error message I get is the following:

 The 3rd argument to function `perform` is causing a mismatch. 44| Task.perform FetchFail FetchSucceed (Http.get commentsCollectionDecoder url) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Function `perform` is expecting the 3rd argument to be: Task.Task Http.Error String But it is: Task.Task Http.Error (List Comment.Model) 
+2
elm
source share
1 answer

I think I understood my problem.

Messages that I define do not have the correct types. The FetchSucceed message should accept (List Comment.Model) , not String . This means that the arguments to the update function must be reflected, and the model will be updated differently.

Something like that:

 type Msg = RetrieveComments | FetchSucceed (List Comment.Model) | FetchFail Http.Error update msg model = case msg of RetrieveComments -> (model, retrieveComments) FetchSucceed newComments -> ({ model | comments = newComments }, Cmd.none) FetchFail error -> (model, Cmd.none) 
+2
source share

All Articles