2017-04-03 8 views
0

httpリクエストから入ってきたjsonをデコードしようとしていますが、私は構文上の問題を抱えています。これは私がコンパイラから取得エラーです:ここではElm Json Decoderパイプラインエラー

-- TYPE MISMATCH ------------------------------------------------------ [7/1811$ 

The 2nd argument to function `send` is causing a mismatch. 

65|  Http.send CardFetch (Http.get url modelDecoder) 
          ^^^^^^^^^^^^^^^^^^^^^^^^^ 
Function `send` is expecting the 2nd argument to be: 

    Http.Request String 

But it is: 

    Http.Request Model 

は私のコードです:

module Main exposing (..) 

import Html exposing (..) 
import Html.Attributes exposing (..) 
import Html.Events exposing (..) 
import Http 
import Json.Decode as Decode exposing (string, Decoder, at, index) 
import Json.Decode.Pipeline exposing (..) 


main : Program Never Model Msg 
main = 
    program 
     { init = init 
     , view = view 
     , update = update 
     , subscriptions = subscriptions 
     } 


type alias Model = 
    { boardName : String 
    , cardName : String 
    } 


init = 
    (Model "Default Board" "Default Card" 
    , Cmd.none 
    ) 



-- UPDATE 


type Msg 
    = CardFetch (Result Http.Error String) 
    | DataFetch 


update : Msg -> Model -> (Model, Cmd Msg) 
update msg model = 
    case msg of 
     DataFetch -> 
      (model, getData) 

     CardFetch (Ok incomingName) -> 
      (Model model.cardName incomingName, Cmd.none) 

     CardFetch (Err errorMessage) -> 
      (model, Debug.log "Errors" Cmd.none) 



-- HTTP 


url : String 
url = 
    "https://api.trello.com/1/members/user/actions?limit=3&key=..." 


getData = 
    Http.send CardFetch (Http.get url modelDecoder) 



{--decodeCard = 
    Decode.index 0 
     modelDecoder 
     (Decode.at 
      [ "data", "card", "name" ] 
      string 
     ) 
     --} 


modelDecoder : Decoder Model 
modelDecoder = 
    decode Model 
     |> custom (index 0 (at [ "data", "card", "name" ] string)) 
     |> custom (index 0 (at [ "data", "board", "name" ] string)) 



--UPDATE 
-- VIEW 


view : Model -> Html Msg 
view model = 
    div [] 
     [ div [] 
      [ button [ onClick DataFetch ] [ text "Get Card" ] ] 
     , div [ class "card" ] 
      [ h3 [] [ text model.boardName ] 
      , div [ class "board" ] [ h4 [] [ text model.cardName ] ] 
      ] 
     ] 



-- SUBSCRIPTIONS 


subscriptions : Model -> Sub Msg 
subscriptions model = 
    Sub.none 

私はエルムにかなり新しいですし、私はAPI呼び出しがどのように動作するかを把握しようとしています。 Elmのドキュメントは優れていますが、API呼び出しに関するビットはあいまいです。私が得ることができるすべての助けに感謝します。どうもありがとう!

答えて

3

あなたはあなたのメッセージに宣言:

正常な応答が文字列を生成することを意味します
CardFetch (Result Http.Error String) 

。ただし、modelDecoderModelmodelDecoder : Decoder Modelです。

CardFetch (Result Http.Error Model) 

と更新機能に:

にメッセージ宣言を変更

CardFetch (Ok incomingName) -> 
    (incomingName, Cmd.none) 

が役立つはずです。