0
Maybeレスポンスの処理方法がわからないので、誰かがTask.performを手伝ってくれることを願っています。Elm 0.17:Task.perform and maybe
私のmodel
には、results
があります。Maybe
のリストまたはNothing
です。
-- model
type alias Item =
{ name : String}
type alias Model =
{ results : Maybe (List Item) }
model = {
results = Nothing
}
私は、タスクを実行し、そのようにそれをデコード:
-- Task
fetch : String -> Cmd Msg
fetch query =
let url =
"https://some_url" ++ query
in
Task.perform FetchFail FetchSuccess (Http.get decode url)
-- decoder
decoder: Json.Decoder (List Item)
decoder =
Json.at ["data"] (Json.list nestedListDecoder)
-- nestedListDecoder
nestedListDecoder : Json.Decoder Item
nestedListDecoder =
Json.object1 Item
("name" := Json.string)
私は、その後のアップデートで応答ハンドル:
-- update
type Msg
= FetchSuccess (Maybe (List Item))
| FetchFail Http.Error
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
FetchSuccess results ->
case results of
Nothing ->
({ model | results = Nothing}, Cmd.none)
Just res ->
({ model | results = res }, Cmd.none)
FetchFail err ->
-- ... handle error
を、ビューに多分に応える:
-- view
result : Item -> Html Msg
result item =
li [] [ text item.name ]
view : Model -> Html Msg
view model =
ul [ ] (List.map result (Maybe.withDefault [] model.results))
このエラーが発生していますまたは結果のMaybe
を扱うとき。
198| Task.perform FetchFail FetchSuccess (Http.get repos url)
^^^^^^^^^^^^^^^^^^
Function `perform` is expecting the 3rd argument to be:
Task Http.Error (Maybe (List Repo))
But it is:
Task Http.Error (List Repo)
Maybe
に対応する必要がある他の人は誰でも助言できますか?
GAH!ありがとうございました。 –