f# - 使用寓言中的节点模块

我正在尝试在我的寓言代码中导入一个节点模块。作为寓言的新手,我确实预料到会出现问题,而理解导入流程似乎就是其中之一。我有下面的代码,编译正常但运​​行时失败,Cannot read property 'request' of undefined on the line of the printfn statement

module Session =
    let inline f (f: 'a->'b->'c->'d) = Func<_,_,_,_> f
    [<Import("default","request")>]
    type Http = 
    abstract request : string -> System.Func<obj,obj,obj,unit> -> unit
    let http : Http = failwith "js only"
    let start () = 

       http.request "http://dr.dk" (ff (fun error response body ->
           printfn "%A" body 
       ))
    do  
        start()

最佳答案

我能够让您的示例在 Fable REPL 中运行:

open System
open Fable
open Fable.Core
open Fable.Core.JS
open Fable.Core.JsInterop

type RequestCallback = Func<obj, obj, obj, unit>

type Request = Func<string, RequestCallback, unit>

[<ImportDefault("request")>]
let request : Request = jsNative

let start () = 
    request.Invoke("http://dr.dk", (fun error response body ->
        console.log("error", error)
        console.log("response", response)
        console.log("body", body)
    ))

start ()

这是它生成的 JavaScript:

import request from "request";
import { some } from "fable-library/Option.js";

export function start() {
    request("http://dr.dk", (error, response, body) => {
        console.log(some("error"), error);
        console.log(some("response"), response);
        console.log(some("body"), body);
    });
}

start();

请注意,许多模块已经有了绑定(bind)。对于这个特定任务,我建议使用 Fable.Fetch .如果您想要一个在浏览器和 .NET 中运行的库,请尝试 Fable.SimpleHttp .

https://stackoverflow.com/questions/38850702/

相关文章:

java - 从另一个项目注入(inject) FeignClient 时出错

c++ - 如何从 MAC 中的/Library/Framework 文件夹中包含 C++ 中的文件

mysql - 如何匹配两个表之间varchar字段中的字符串

python - 在 Python 中查找对称矩阵的 SVD

android - 键盘打开android时布局正在缩小

c# - 使用 Entity Framework 定义关系/FK 的两端是否重要?

android - 如果暂停时间太长,为什么应用程序无法从最近启动

google-maps - 谷歌地图 -> 根据给定距离制作环形路线?

reactjs - 每个 child 在 React 中应该有一个唯一的关键错误

c# - WCF 服务在多服务器环境中不起作用