榆树Http Api电话

时间:2017-04-01 23:15:02

标签: http elm

我对榆树来说还不够新鲜。我目前正在尝试自学如何在Elm中进行API调用。我正在制作的程序非常简单,它会发出一个http请求来获取一些Trello卡信息并显示它。我的程序编译但单击按钮获取信息时没有任何反应。我不确定我做错了什么,请帮忙。感谢

 module Main exposing (..)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode as Decode


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)
    | FetchCard


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        FetchCard ->
            ( model, getCardName )

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

        CardFetch (Err _) ->
            ( model, Cmd.none )



-- HTTP


getCardName =
    Http.send CardFetch (Http.get "https://api.trello.com/1/members/..." decodeCard)


decodeCard =
    Decode.at [ "data", "card", "name" ] Decode.string



--UPDATE
-- VIEW


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



-- SUBSCRIPTIONS


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

1 个答案:

答案 0 :(得分:1)

终于明白了。我不得不将它解码为Decode.index 0的数组(Decode.at [“data”,“card”,“name”] Decode.string)