Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elm STARTAP解码http请求_Elm - Fatal编程技术网

Elm STARTAP解码http请求

Elm STARTAP解码http请求,elm,Elm,我正在尝试在Elm中解码http请求,使用StartApp作为基础。虽然我遇到了一个错误,但我真的不知道如何修复: The right argument of (|>) is causing a type mismatch. 76│ Http.getString testUrl 77│ |> Task.map parseMon 78│> |> Task.map OnPokemonLoaded (|>) is expecting the ri

我正在尝试在Elm中解码http请求,使用StartApp作为基础。虽然我遇到了一个错误,但我真的不知道如何修复:

The right argument of (|>) is causing a type mismatch.

76│     Http.getString testUrl
77│     |> Task.map parseMon
78│>    |> Task.map OnPokemonLoaded

(|>) is expecting the right argument to be a:

    Task Http.Error (Result String Pokemon) -> a

But the right argument is:

    Task Http.Error (Result Http.Error Pokemon) -> Task Http.Error Action
它所说的代码是:

-- Fetching test mon
testUrl : String
testUrl = "http://pokeapi.co/api/v2/pokemon/1/"


fetchTest : Effects.Effects Action
fetchTest =
    Http.getString testUrl
    |> Task.map parseMon
    |> Task.map OnPokemonLoaded --line 78
    |> Effects.task

parseMon : String -> Result String Pokemon.Pokemon
parseMon json = Json.Decode.decodeString Pokemon.decoder json
OnPokemonLoaded
是我的操作之一:
OnPokemonLoaded(Result Http.Error Pokemon)
Pokemon.decoder
是一个简单的json解码器:
decoder:decoder Pokemon

我对Elm还是新手,只是在尝试StartApp和Effects。这个错误似乎很好地解释了这个问题,但我仍然有点不知道它应该如何工作


那么,我应该如何正确地请求和解码json呢?

不需要使用
Http.getString
parseMon
。相反,您可以使用
Http.get
并传递Json解码器,然后将其映射到
结果
,以获得所需的功能:

fetchTest:Effects.Effects操作
取回测试=
Http.get Pokemon.decoder testUrl
|>Task.toResult
|>Task.map OnPokemonLoaded
|>效果任务

谢谢!我在学习一个教程,他们使用了getString(但没有解码),完全考察了还有一个简单的
get
函数的事实:P