2016-09-21 1 views
2

私は自分の小さなプロジェクトにElmチュートリアルを適用しようとしており、私が提供しているJson.Decoderに問題があります。Task.performは3番目の引数が別の型になることを期待しています

私のコードは次のようになります。

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) 

モデルは、2つのフィールド、authorcontentとちょうどレコードです。

私は取得していますエラーメッセージはこれです:

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) 
+0

おそらく関連:http://stackoverflow.com/questions/39320948/decode-json-array-with-objects-in-elm –

答えて

2

私は、私は私の問題を考え出したと思います。

私が定義しているメッセージに正しいタイプがありません。 FetchSucceedメッセージはStringではなく(List Comment.Model)を受け入れる必要があります。つまり、update関数の引数を反映させる必要があり、モデルは別の方法で更新されます。このような

何か:

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) 
関連する問題