0
아마 Maybe 응답을 처리하는 방법을 정말로 이해하지 못해서 누군가 Task.perform을 도울 수 있기를 바라고 있습니다. 그리고 문서는 나를 위해 더 명확하게 보이지 않습니다.느릅 나무 0.17 : Task.perform 그리고 아마도
내 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
인 경우. 나는 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)
는 사람이 조언을 할 수 있습니까?
GAH를! 고맙습니다. –