当参数与工作示例相同时,为什么 Elm 不编译此 JSON 数据示例的 HTTP 请求?
Why won't Elm compile this HTTP request for JSON data example when the arguments are the same as working examples?
我正在研究一个基本的解码-json Elm 示例用于练习,但无法找出 Elm 编译错误。我也很困惑为什么它在 Ellie 中运行(即使用远程 JSON URL),但在使用 Elm v 19.0 FYI 在本地编译时却不运行。
目标是进行一个简单的调用以从 Go 服务器获取 JSON,但仅编译我从文档中获得的用于解码 JSON 的示例 Elm 无法正常工作,所以我们在这里.
module HelloWorld exposing (..)
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode exposing (Decoder, field, string)
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type Model
= Failure
| Loading
| Success String
init : () -> (Model, Cmd Msg)
init _ =
(Loading, getRandomCatGif)
-- UPDATE
type Msg
= MorePlease
| GotGif (Result Http.Error String)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
MorePlease ->
(Loading, getRandomCatGif)
GotGif result ->
case result of
Ok url ->
(Success url, Cmd.none)
Err _ ->
(Failure, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h2 [] [ text "Random Cats" ]
, viewGif model
]
viewGif : Model -> Html Msg
viewGif model =
case model of
Failure ->
div []
[ text "I could not load a random cat for some reason. "
, button [ onClick MorePlease ] [ text "Try Again!" ]
]
Loading ->
text "Loading..."
Success url ->
div []
[ button [ onClick MorePlease, style "display" "block" ] [ text "More Please!" ]
, img [ src url ] []
]
-- HTTP
getRandomCatGif : Cmd Msg
getRandomCatGif =
Http.get
{ url = "http://127.0.0.1:8080/test"
, expect = Http.expectJson GotGif gifDecoder
}
gifDecoder : Decoder String
gifDecoder =
field "data" (field "image_url" string)
现在是 go 服务器
// write json back to elm call
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/schema"
)
type ToDo struct {
Title string
Description string
}
func main() {
http.HandleFunc("/test", test)
http.ListenAndServe(getPort(), nil)
}
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":8080"
}
func test(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
//Allow CORS here By * or specific origin
w.Header().Set("Access-Control-Allow-Origin", "*")
// say hi
fmt.Fprintf(w, "hello from go"+"\n")
} else {
//Allow CORS here By * or specific origin
w.Header().Set("Access-Control-Allow-Origin", "*")
todo := readForm(r)
fmt.Fprintf(w, "updated todo to"+todo.Title+"\n")
}
}
func readForm(r *http.Request) *ToDo {
r.ParseForm()
todo := new(ToDo)
decoder := schema.NewDecoder()
decodeErr := decoder.Decode(todo, r.PostForm)
if decodeErr != nil {
log.Printf("error mapping parsed form data to struct : ", decodeErr)
}
return todo
}
最后是 ELM 错误
榆树制作 ./src/HelloWorld.elm
Detected problems in 1 module.
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
Something is off with the body of the `getRandomCatGif` definition:
110|> Http.get
111|> { url = "http://127.0.0.1:8080/test"
112|> , expect = Http.expectJson GotGif gifDecoder
113|> }
This `get` call produces:
Decoder a -> Http.Request a
But the type annotation on `getRandomCatGif` says it should be:
Cmd Msg
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
The 1st argument to `get` is not what I expect:
110| Http.get
111|> { url = "http://127.0.0.1:8080/test"
112|> , expect = Http.expectJson GotGif gifDecoder
113|> }
This argument is a record of type:
{ expect : b, url : String }
But `get` needs the 1st argument to be:
String
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
The 1st argument to `expectJson` is not what I expect:
112| , expect = Http.expectJson GotGif gifDecoder
^^^^^^
This `GotGif` value is a:
Result Http.Error String -> Msg
But `expectJson` needs the 1st argument to be:
Decoder a
-- TOO MANY ARGS -------------------------------------------- src/HelloWorld.elm
The `expectJson` function expects 1 argument, but it got 2 instead.
112| , expect = Http.expectJson GotGif gifDecoder
^^^^^^^^^^^^^^^
Are there any missing commas? Or missing parentheses?
您似乎已经安装了 elm/http
模块的 1.0.0 版,而该示例需要 elm/http
2.0.0 版。我可以用这个包的 1.0.0 版重现你的错误,但是代码用 2.0.0.
编译成功
编辑您的 elm.json
文件,将 elm/http
的版本更改为 2.0.0
并再次尝试 运行 elm make ...
。
我正在研究一个基本的解码-json Elm 示例用于练习,但无法找出 Elm 编译错误。我也很困惑为什么它在 Ellie 中运行(即使用远程 JSON URL),但在使用 Elm v 19.0 FYI 在本地编译时却不运行。
目标是进行一个简单的调用以从 Go 服务器获取 JSON,但仅编译我从文档中获得的用于解码 JSON 的示例 Elm 无法正常工作,所以我们在这里.
module HelloWorld exposing (..)
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Http
import Json.Decode exposing (Decoder, field, string)
-- MAIN
main =
Browser.element
{ init = init
, update = update
, subscriptions = subscriptions
, view = view
}
-- MODEL
type Model
= Failure
| Loading
| Success String
init : () -> (Model, Cmd Msg)
init _ =
(Loading, getRandomCatGif)
-- UPDATE
type Msg
= MorePlease
| GotGif (Result Http.Error String)
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
MorePlease ->
(Loading, getRandomCatGif)
GotGif result ->
case result of
Ok url ->
(Success url, Cmd.none)
Err _ ->
(Failure, Cmd.none)
-- SUBSCRIPTIONS
subscriptions : Model -> Sub Msg
subscriptions model =
Sub.none
-- VIEW
view : Model -> Html Msg
view model =
div []
[ h2 [] [ text "Random Cats" ]
, viewGif model
]
viewGif : Model -> Html Msg
viewGif model =
case model of
Failure ->
div []
[ text "I could not load a random cat for some reason. "
, button [ onClick MorePlease ] [ text "Try Again!" ]
]
Loading ->
text "Loading..."
Success url ->
div []
[ button [ onClick MorePlease, style "display" "block" ] [ text "More Please!" ]
, img [ src url ] []
]
-- HTTP
getRandomCatGif : Cmd Msg
getRandomCatGif =
Http.get
{ url = "http://127.0.0.1:8080/test"
, expect = Http.expectJson GotGif gifDecoder
}
gifDecoder : Decoder String
gifDecoder =
field "data" (field "image_url" string)
现在是 go 服务器
// write json back to elm call
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/schema"
)
type ToDo struct {
Title string
Description string
}
func main() {
http.HandleFunc("/test", test)
http.ListenAndServe(getPort(), nil)
}
func getPort() string {
p := os.Getenv("PORT")
if p != "" {
return ":" + p
}
return ":8080"
}
func test(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
//Allow CORS here By * or specific origin
w.Header().Set("Access-Control-Allow-Origin", "*")
// say hi
fmt.Fprintf(w, "hello from go"+"\n")
} else {
//Allow CORS here By * or specific origin
w.Header().Set("Access-Control-Allow-Origin", "*")
todo := readForm(r)
fmt.Fprintf(w, "updated todo to"+todo.Title+"\n")
}
}
func readForm(r *http.Request) *ToDo {
r.ParseForm()
todo := new(ToDo)
decoder := schema.NewDecoder()
decodeErr := decoder.Decode(todo, r.PostForm)
if decodeErr != nil {
log.Printf("error mapping parsed form data to struct : ", decodeErr)
}
return todo
}
最后是 ELM 错误 榆树制作 ./src/HelloWorld.elm
Detected problems in 1 module.
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
Something is off with the body of the `getRandomCatGif` definition:
110|> Http.get
111|> { url = "http://127.0.0.1:8080/test"
112|> , expect = Http.expectJson GotGif gifDecoder
113|> }
This `get` call produces:
Decoder a -> Http.Request a
But the type annotation on `getRandomCatGif` says it should be:
Cmd Msg
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
The 1st argument to `get` is not what I expect:
110| Http.get
111|> { url = "http://127.0.0.1:8080/test"
112|> , expect = Http.expectJson GotGif gifDecoder
113|> }
This argument is a record of type:
{ expect : b, url : String }
But `get` needs the 1st argument to be:
String
-- TYPE MISMATCH -------------------------------------------- src/HelloWorld.elm
The 1st argument to `expectJson` is not what I expect:
112| , expect = Http.expectJson GotGif gifDecoder
^^^^^^
This `GotGif` value is a:
Result Http.Error String -> Msg
But `expectJson` needs the 1st argument to be:
Decoder a
-- TOO MANY ARGS -------------------------------------------- src/HelloWorld.elm
The `expectJson` function expects 1 argument, but it got 2 instead.
112| , expect = Http.expectJson GotGif gifDecoder
^^^^^^^^^^^^^^^
Are there any missing commas? Or missing parentheses?
您似乎已经安装了 elm/http
模块的 1.0.0 版,而该示例需要 elm/http
2.0.0 版。我可以用这个包的 1.0.0 版重现你的错误,但是代码用 2.0.0.
编辑您的 elm.json
文件,将 elm/http
的版本更改为 2.0.0
并再次尝试 运行 elm make ...
。