【问题标题】:Fable/Thoth compile error: toJson is not definedFable/Thoth 编译错误:未定义 toJson
【发布时间】:2020-05-24 05:06:47
【问题描述】:

我正在尝试使用 Fable 发出 POST 请求,遵循本教程:https://fable.io/fable-powerpack/posts/fetch.html

我得到这个编译错误:

error FSHARP: The value or constructor 'toJson' is not defined. Maybe you want one of the following:
       JSON (code 39)
     @ ./UI/UI.fsproj 1:0-24 1:0-24

这是我的代码:

let submitForm = 
    let txtName = getInputElementById("txtName")
    let postData = { Name = txtName.value }

    let requestProps =
        [ RequestProperties.Method HttpMethod.POST
        ; requestHeaders [ContentType "application/json"]
        ; RequestProperties.Body (unbox (toJson postData))
        ]
        //Encode.Auto.toString(1, postData)
        //toJson    
        //Fable.Core.JS.JSON.stringify

    promise {
        let! response = fetch "http://localhost:5000/employee/create" requestProps
        let responseText = response.text()
        Browser.console.log responseText
    } |> ignore

我在 Fable/.fable/Thoth.Json.2.0.0/Encode.fs 中也遇到了这些编译错误:

The value, constructor, namespace or type 'Array' is not defined. (code 39)
The value, constructor, namespace or type 'JSON' is not defined. 

我猜这是因为 Fable 引用的是 Thoth 2.0.0,而不是最新版本。我该如何解决这个错误?

https://github.com/jaydpather/FunctionalCrudForms/blob/master/Fable/UI/UI.fs

【问题讨论】:

    标签: post fetch fable


    【解决方案1】:

    免责声明我是 Thoth.Json 的维护者。

    您似乎在使用 2 年以上的 Fable 版本。

    fable-powerpack 已被弃用,并已拆分为不同的包。但是我们好像忘记更新这个网站了。

    对于 Fetch API,它现在位于包 Fable.Fetch 中。

    toJson 已不存在,建议使用Thoth.JsonFable.SimpleJson

    还有包 Thoth.Fetch 已经将 Fetch 和 Thoth.Json 粘合在一起,因此您不必编写(反)序列化部分。

    在你的情况下,它会给出类似的东西:

    let createBook () : JS.Promise<unit> =
        promise {
            let url = "http://localhost:5000/employee/create"
            let txtName = getInputElementById("txtName")
            let data =
                {| Name = = txtName .Value |} // Using an anonymous for simplicity
    
            return! Fetch.post(url, data, caseStrategy = CamelCase)
        }
    

    【讨论】:

    • 嗨马克西姆,感谢您的回答。 Fable 是一个很棒的 AP​​I,但如果你有一个完整的示例应用程序,它会很有帮助。
    • 您好,谢谢您的好话:) 我们有几个应用程序可以作为示例。例如,fulma-demo 是一个问答应用程序,它展示了 Elmish、Thoth.Json、Fetch 的用法github.com/MangelMaxime/fulma-demo 还有“The Elmish book”,它从基本的 Fable 示例到复杂的 Elmish 应用程序一步一步地练习:zaid-ajaj.github.io/the-elmish-book Fable Awesome 也可以帮助找到更多资源:github.com/kunjee17/awesome-fable 但事实上,文档仍然很粗糙,需要大量时间来改进。希望,它有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2013-12-25
    相关资源
    最近更新 更多