如何从 clojurescript 调用 node.js 模块中的 javascript?
How to call javascript in a node.js module from clojurescript?
我希望在 ClojureScript 中使用 Coinbase Bitcoin Exchange node.js API。
目标是复制页面上的第一个 javascript 示例:
var CoinbaseExchange = require('coinbase-exchange');
var publicClient = new CoinbaseExchange.PublicClient();
但在我的以下代码中,我开始尝试访问 PublicClient
:
(ns plutus.core
(:require
[cljs.nodejs :as nodejs]))
(def coinb (nodejs/require "coinbase-exchange"))
(nodejs/enable-util-print!)
(defn -main [& args]
(def pc (js/PublicClient.)))
(println pc))
(set! *main-cli-fn* -main)
这会抛出一个 ReferenceError: PublicClient is not defined
,尽管我也尝试过(以及类似的变体):
(def pc (. coinb (PublicClient)))
(def pc (.PublicClient coinb))
(def pc (:PublicClient coinb))
打印 nil
所有这些都失败了。我已经非常仔细地研究了 this article 的任何相关示例,但我对使用 node.js 如何影响事物的命名感到困惑,如果有的话。
真的不是命名问题,更多的是关于如何在对象中使用 new
和嵌套属性。不幸的是,您无法在事后获得对 PublicClient
和 new
的引用。
(new (.-PublicClient coinb))
即使 (.-PublicClient coinb)
returns 函数也不起作用。
您需要通过在 ClojureScript 中使用点符号来指向函数的位置:
(new coinb.PublicClient)
;; OR, more idiomatic
(coinb.PublicClient.)
那应该给你你想要的。
还有其他人看到了吗?
> ((.-getProducts (cb.PublicClient.)) (fn [_ _ _] nil))
#object[TypeError TypeError: self.makeRelativeURI is not a function]
我希望在 ClojureScript 中使用 Coinbase Bitcoin Exchange node.js API。
目标是复制页面上的第一个 javascript 示例:
var CoinbaseExchange = require('coinbase-exchange');
var publicClient = new CoinbaseExchange.PublicClient();
但在我的以下代码中,我开始尝试访问 PublicClient
:
(ns plutus.core
(:require
[cljs.nodejs :as nodejs]))
(def coinb (nodejs/require "coinbase-exchange"))
(nodejs/enable-util-print!)
(defn -main [& args]
(def pc (js/PublicClient.)))
(println pc))
(set! *main-cli-fn* -main)
这会抛出一个 ReferenceError: PublicClient is not defined
,尽管我也尝试过(以及类似的变体):
(def pc (. coinb (PublicClient)))
(def pc (.PublicClient coinb))
(def pc (:PublicClient coinb))
打印 nil
所有这些都失败了。我已经非常仔细地研究了 this article 的任何相关示例,但我对使用 node.js 如何影响事物的命名感到困惑,如果有的话。
真的不是命名问题,更多的是关于如何在对象中使用 new
和嵌套属性。不幸的是,您无法在事后获得对 PublicClient
和 new
的引用。
(new (.-PublicClient coinb))
即使 (.-PublicClient coinb)
returns 函数也不起作用。
您需要通过在 ClojureScript 中使用点符号来指向函数的位置:
(new coinb.PublicClient)
;; OR, more idiomatic
(coinb.PublicClient.)
那应该给你你想要的。
还有其他人看到了吗?
> ((.-getProducts (cb.PublicClient.)) (fn [_ _ _] nil))
#object[TypeError TypeError: self.makeRelativeURI is not a function]