如何在clojurescript的node.js模块中调用javascript?

我希望在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 ,尽pipe我也尝试过(以及类似的变化):

  • (def pc (. coinb (PublicClient)))
  • (def pc (.PublicClient coinb))
  • (def pc (:PublicClient coinb))

所有这些都没有通过打印nil 。 我已经仔细研究了这篇文章的任何相关示例,但是我很困惑如何使用node.js影响事物的命名。

真的不是一个命名问题,更多的是关于如何在对象中使用new的嵌套属性。 不幸的是,您无法获得对PublicClient的引用,然后在事实之后再获得new引用。

 (new (.-PublicClient coinb)) 

即使(.-PublicClient coinb)返回一个函数,它也不起作用。

你需要在ClojureScript中使用点符号来指向函数的位置:

 (new coinb.PublicClient) ;; OR, more idiomatic (coinb.PublicClient.) 

这应该给你你想要的。

其他人看到这个?

 > ((.-getProducts (cb.PublicClient.)) (fn [_ _ _] nil)) #object[TypeError TypeError: self.makeRelativeURI is not a function]