在这个 clojure 项目中,我的命名空间声明有什么问题?

What is wrong with my namespace declaration in this clojure project?

提问人:Pedro Delfino 提问时间:8/19/2023 更新时间:8/26/2023 访问量:47

问:

我正在使用 Clojure 和 leinigen 来构建一个后端项目。

这是我的文件的简化版本:

(ns simplified...
  (:require [ring.middleware.json :refer [wrap-json-response]]
            [ring.util.response :refer [response]]
            [ring.middleware.json :refer [wrap-json-body]]
            [ring.middleware.defaults :refer [wrap-defaults]]
            [ring.adapter.jetty :as jetty]))

(defroutes rest-api-routes
  (POST "/api" req
        
        (clojure.pprint/pprint req)
        "hello"))

(defn import-atb
  [request-id tenant-id]
  (let [trial-balance-report  (middleware/wrap-json-body rest-api-routes {:keywords? true})])
  trial-balance-report)

做完 后,当我运行时,我得到:lein cleanlein repl

#error {
 :cause No such namespace: middleware

我的文件有什么问题?

Clojure 命名空间 中间件 Leiningen Ring

评论

6赞 Harold 8/19/2023
在你写之前,你需要一个你需要的向量。不过,看起来你已经尝试过了,所以,也许只是改成?middleware/wrap-json-body:as middleware:referwrap-json-bodymiddleware/wrap-json-bodywrap-json-body

答:

2赞 Nate Smith 8/26/2023 #1

快速解决方法是替换它,因为它已被引用到命名空间中。middleware/wrap-json-bodywrap-json-body

require可以采取选择。您在此处使用的选项允许您直接从自己的命名空间引用该命名空间的 var。该选项将允许您在自己的命名空间中设置别名。例如,您可以在命名空间中引用。:refer:refer:as(:require [ring.middleware.json :as ring-json])ring-json/wrap-json-body