首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Fable/Thoth编译错误:未定义toJson

Fable/Thoth编译错误:未定义toJson
EN

Stack Overflow用户
提问于 2020-05-24 13:06:48
回答 1查看 63关注 0票数 0

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

我得到了这个编译错误:

代码语言:javascript
复制
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

下面是我的代码:

代码语言:javascript
复制
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中也得到了这些编译错误:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-26 03:40:01

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

看起来您使用的是2+版本的寓言。

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

在使用Fetch API的情况下,它现在位于包Fable.Fetch中。

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

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

在你的例子中,它会给出类似这样的东西:

代码语言:javascript
复制
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)
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61981877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档