我解码来自Http.post请求的响应,返回类型为Integer:
responseDecoder : Decoder Int
responseDecoder =
field "data" (field "createDeadline" (field "id" int))
我的问题是我使用需要String的Http.send :
createDeadline value =
Http.send Resolved (Http.post deadlineUrl (encodeBody value |> Http.jsonBody) responseDecoder)
而且我不知道如何更改返回类型。我的错误消息如下:
The 2nd argument to `send` is not what I expect:
113| Http.send Resolved (Http.post deadlineUrl (encodeBody value |> Http.jsonBody) responseDecoder)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This `post` call produces:
Http.Request Int
But `send` needs the 2nd argument to be:
Http.Request String
Hint: I always figure out the argument types from left to right. If an argument
is acceptable, I assume it is “correct” and move on. So the problem may actually
be in one of the previous arguments!
Hint: Want to convert an Int into a String? Use the String.fromInt function!
有人能帮忙吗?我只是在和榆木玩耍,但我被困在这里。
我的问题是我使用需要String的Http.send
根据docs,send
函数要求参数为type Request a
,其中a
可以为任何类型(Int
或String
其他类型)。
您遇到的问题就是编译错误提示中所说的:
提示:我总是从左到右找出参数类型。如果一个参数可以接受,我认为它是“正确的”并继续。因此,问题可能出在先前的争论之一中!
因此,您似乎已经在某个地方定义了您所期望的String
,并且编译器将类型推断为Request String
。例如,您可能已Resolved
定义如下内容:
type Msg = Resolved (Result Http.Error String)
并且编译器推断出send : (Result Error a -> msg) -> Request a -> Cmd msg
特定类型的多态类型,因为它已经看到第一个参数是or type (Result Error String -> msg)
:
send : (Result Error String -> msg) -> Request String -> Cmd msg
因此,在这种情况下,解决方案是更改期望的类型:
type Msg = Resolved (Result Http.Error Int)
或更改解码器并将响应解码为String
:
responseDecoder : Decoder String
responseDecoder =
Json.Decode.map String.fromInt (field "data" (field "createDeadline" (field "id" int)))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句