Task.perform期望第三个参数为其他类型 [英] Task.perform is expecting the 3rd argument to be a different type
问题描述
我正在尝试将Elm教程改编成自己的小项目,而我所提供的Json.Decoder遇到了麻烦。
I'm trying to adapt the Elm tutorial to my own little project and I'm running into trouble with the Json.Decoder that I am supplying.
我的代码如下:
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
.
错误消息I '得到的是这样的:
The error message I'm getting is this:
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)
推荐答案
我想我已经解决了我的问题。
I think I figured out my problem.
我定义的消息类型不正确。 FetchSucceed
消息应该接受(列表注释模型)
,而不是 String
。这意味着 update
函数的参数需要反映,并且模型将以不同的方式进行更新。
The messages that I'm defining do not have the correct types. The FetchSucceed
message should be accepting a (List Comment.Model)
rather than a String
. Which means the update
function's arguments need to reflect and the model will be updated in a different way.
类似
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)
这篇关于Task.perform期望第三个参数为其他类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!