“使用 Clojure 进行 Web 开发”:没有这样的变量:db/get-user

Posted

技术标签:

【中文标题】“使用 Clojure 进行 Web 开发”:没有这样的变量:db/get-user【英文标题】:"Web Development with Clojure": No such var: db/get-user 【发布时间】:2014-03-10 05:52:27 【问题描述】:

我正在使用“使用 Clojure 进行 Web 开发”一书来制作一个教程留言簿应用程序,但遇到了麻烦。似乎一些库和它们的功能自本书发行以来已经更新,这对我来说对 Web 开发的介绍有点颠簸。我已经能够通过更新依赖项来解决大多数问题,或者在我不能的时候使用旧版本的库,但是这种策略已经达到了极限,我遇到了一个棘手的问题。当我尝试在本地主机服务器上运行我的应用程序时,我得到了这个堆栈跟踪:

Exception in thread "main" java.lang.RuntimeException: No such var: db/get-user, compiling:(guestbook/routes/auth.clj:38:14)
    at clojure.lang.Compiler.analyze(Compiler.java:6380)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3573)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6562)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.access$100(Compiler.java:37)
    at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5973)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6560)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6548)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5708)
    at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5139)
    at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3751)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6558)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6548)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.access$100(Compiler.java:37)
    at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:529)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6560)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler.compile1(Compiler.java:7148)
    at clojure.lang.Compiler.compile(Compiler.java:7219)
    at clojure.lang.RT.compile(RT.java:398)
    at clojure.lang.RT.load(RT.java:438)
    at clojure.lang.RT.load(RT.java:411)
    at clojure.core$load$fn__5018.invoke(core.clj:5530)
    at clojure.core$load.doInvoke(core.clj:5529)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.core$load_one.invoke(core.clj:5336)
    at clojure.core$load_lib$fn__4967.invoke(core.clj:5375)
    at clojure.core$load_lib.doInvoke(core.clj:5374)
    at clojure.lang.RestFn.applyTo(RestFn.java:142)
    at clojure.core$apply.invoke(core.clj:619)
    at clojure.core$load_libs.doInvoke(core.clj:5413)
    at clojure.lang.RestFn.applyTo(RestFn.java:137)
    at clojure.core$apply.invoke(core.clj:619)
    at clojure.core$require.doInvoke(core.clj:5496)
    at clojure.lang.RestFn.invoke(RestFn.java:457)
    at guestbook.handler$loading__4910__auto__.invoke(handler.clj:1)
    at clojure.lang.AFn.applyToHelper(AFn.java:159)
    at clojure.lang.AFn.applyTo(AFn.java:151)
    at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:3458)
    at clojure.lang.Compiler.compile1(Compiler.java:7153)
    at clojure.lang.Compiler.compile1(Compiler.java:7143)
    at clojure.lang.Compiler.compile(Compiler.java:7219)
    at clojure.lang.RT.compile(RT.java:398)
    at clojure.lang.RT.load(RT.java:438)
    at clojure.lang.RT.load(RT.java:411)
    at clojure.core$load$fn__5018.invoke(core.clj:5530)
    at clojure.core$load.doInvoke(core.clj:5529)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.core$load_one.invoke(core.clj:5336)
    at clojure.core$compile$fn__5023.invoke(core.clj:5541)
    at clojure.core$compile.invoke(core.clj:5540)
    at user$eval19.invoke(form-init6949279956252999274.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6619)
    at clojure.lang.Compiler.eval(Compiler.java:6609)
    at clojure.lang.Compiler.load(Compiler.java:7064)
    at clojure.lang.Compiler.loadFile(Compiler.java:7020)
    at clojure.main$load_script.invoke(main.clj:294)
    at clojure.main$init_opt.invoke(main.clj:299)
    at clojure.main$initialize.invoke(main.clj:327)
    at clojure.main$null_opt.invoke(main.clj:362)
    at clojure.main$main.doInvoke(main.clj:440)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:419)
    at clojure.lang.AFn.applyToHelper(AFn.java:163)
    at clojure.lang.Var.applyTo(Var.java:532)
    at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: No such var: db/get-user
    at clojure.lang.Util.runtimeException(Util.java:219)
    at clojure.lang.Compiler.resolveIn(Compiler.java:6848)
    at clojure.lang.Compiler.resolve(Compiler.java:6818)
    at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6779)
    at clojure.lang.Compiler.analyze(Compiler.java:6343)
    ... 72 more
Compilation failed: Subprocess failed

我想我可能在这里加载了一些我不需要的东西,但我的 project.clj 文件看起来像这样:

(defproject guestbook "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [hiccup "1.0.5"]
                 [ring-server "0.3.1"]
                 [lein-light-nrepl "0.0.15"]
                 [org.clojure/clojurescript "0.0-2156"]
                 [org.clojure/java.jdbc "0.2.3"]
                 [org.xerial/sqlite-jdbc"3.7.2"]
                 [lib-noir "0.8.0"]]
  :repl-options :nrepl-middleware [lighttable.nrepl.handler/lighttable-ops]
  :plugins [[lein-ring "0.8.7"]
            [lein-ancient "0.5.4"]]
  :ring :handler guestbook.handler/app
         :init guestbook.handler/init
         :destroy guestbook.handler/destroy
  :aot :all
  :profiles
  :production
   :ring
    :open-browser? false, :stacktraces? false, :auto-reload? false
   :dev
   :dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.2.1"]])

主文件:

(ns guestbook.routes.home
  (:require [compojure.core :refer :all]
            [guestbook.views.layout :as layout]
            [hiccup.form :refer :all]
            [guestbook.models.db :as db]
            [noir.session :as session]))


(defn format-time [timestamp]
  (-> "dd/MM/yyyy"
      (java.text.SimpleDateFormat.)
      (.format timestamp)))


(defn home []
  (layout/common [:h1 "Guestbook"]
                 [:p "Welcome to my guestbook"]
                 [:hr]
                 [:form]
                 [:p "name"]
                 [:input]
                 [:p "Message"]
                 [:textarea :rows 10 :cols 40]))


(defn show-guests []
 [:ul.guests
   (for [:keys [message name timestamp] (db/read-guests)]
    [:li
       [:blockquote message]
       [:p "-" [:cite name]]
       [:time (format-time timestamp)]])])

(defn home [& [name message error]]
  (layout/common
    [:h1 "Guestbook " (session/get :user)]
    [:p "Welcome to my guestbook"]
    [:p error]

    (show-guests)

    [:hr]

    (form-to [:post "/"]
             [:p "Name:" (text-field "name" name)]
             [:p "Message:" (text-area :rows 10 :cols 40 "message" message)]
             (submit-button "comment"))))


(defn save-message [name message]
 (cond
   (empty? name)
   (home name message "You forgot to leave a name, dumbass.")
   (empty? message)
   (home name message "Don't you have something to say?")
   :else
   (do
     (db/save-message name message)
     (home))))



(defroutes home-routes
  (GET "/" [] (home))
  (POST "/" [name message] (save-message name message)))

处理程序:

(ns guestbook.handler
  (:use compojure.core
        ring.middleware.resource
        ring.middleware.file-info
        hiccup.middleware
        guestbook.routes.home)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [guestbook.models.db :as db]
            [guestbook.routes.auth :refer [auth-routes]]))

(defn init []
  (println "guestbook is starting")
  (if-not (.exists (java.io.File. "./db.sq3"))
    (db/create-guestbook-table)))

(defn destroy []
  (println "guestbook is shutting down"))

(defroutes app-routes
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (-> (routes auth-routes home-routes app-routes)
      (handler/site)
      (wrap-base-url)))

数据库:

(ns guestbook.models.db
  (:require [clojure.java.jdbc :as sql])
  (:import java.sql.DriverManager))

(def db :classname  "org.sqlite.JDBC",
         :subprotocol   "sqlite",
         :subname       "db.sq3")

(defn create-guestbook-table []
  (sql/with-connection
    db
    (sql/create-table
      :guestbook
      [:id "INTEGER PRIMARY KEY AUTOINCREMENT"]
      [:timestamp "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"]
      [:name "TEXT"]
      [:message "TEXT"])
    (sql/do-commands "CREATE INDEX timestamp_index ON guestbook (timestamp)")))

(defn read-guests []
  (sql/with-connection
    db
    (sql/with-query-results res
      ["SELECT * FROM guestbook ORDER BY timestamp DESC"]
      (doall res))))

(defn save-message [name message]
  (sql/with-connection
    db
    (sql/insert-values
      :guestbook
      [:name :message :timestamp]
      [name message (new java.util.Date)])))

授权:

(ns guestbook.routes.auth
  (:require [compojure.core :refer [defroutes GET POST]]
            [guestbook.views.layout :as layout]
            [hiccup.form :refer
             [form-to label text-field password-field submit-button]]
            [noir.response :refer [redirect]]
            [noir.session :as session]
            [noir.validation
             :refer [rule errors? has-value? on-error]]
            [noir.util.crypt :as crypt]
            [guestbook.models.db :as db]))

(defn format-error [[error]]
  [:p.error error])

(defn control [field name text]
  (list (on-error name format-error)
        (label name text)
        (field name)
        [:br]))

(defn registration-page []
  (layout/common
    (form-to [:post "/register"]
      (control text-field :id "screen name")
      (control password-field :pass "Password")
      (control password-field :pass1 "Retype Password")
      (submit-button "Create Account"))))

(defn login-page []
  (layout/common
    (form-to [:post "/login"]
      (control text-field :id "screen name")
      (control password-field :pass "Password")
      (submit-button "login"))))

(defn handle-login [id pass]
  (let [user (db/get-user id)]
    (rule (has-value? id)
          [:id "screen name is required"])
    (rule (has-value? pass)
          [:pass "password is required"])
    (rule (and user (crypt/compare pass (:pass user)))
          [:pass "invalid password"])
    (if (errors? :id :pass)
    (login-page)
    (do
      (session/put! :user id)
      (redirect "/")))))

(defn handle-registration [id pass pass1]
  (rule (= pass pass1)
        [:pass "password was not retyped correctly"])
  (if (errors? :pass)
    (registration-page)
    (do
      (db/add-user-record :id id :pass (crypt/encrypt pass))
      (redirect "/login"))))

(defroutes auth-routes
  (GET "/register" [] (registration-page))
  (POST "/register" [id pass pass1]
        (handle-registration id pass pass1))

  (GET "/login" [] (login-page))
  (POST "/login" [id pass]
    (handle-login id pass))

  (GET "/logout" []
        (layout/common
          (form-to [:post "/logout"]
            (submit-button "logout"))))
  (POST "/logout" []
        (session/clear!)
        (redirect "/")))

有人可以帮我解开粘住的东西,这样我就可以重新开始我的教程了吗?此外,任何有关如何根据已弃用的库有效更新代码的提示都会有所帮助。对于冗长的代码,我深表歉意,但我仍然是菜鸟,不确定问题出在哪里。我怀疑它必须与数据库依赖项有关,因为在我的谷歌搜索 [org.clojure/java.jdbc "0.2.3"] 依赖项中,特别是 sql/with-connection 已被弃用,即现在在 0.3.3 版上。

【问题讨论】:

【参考方案1】:

注意堆栈跟踪中的第一行: Exception in thread "main" java.lang.RuntimeException: No such var: db/get-user, compiling:(guestbook/routes/auth.clj:38:14)

编译 auth.clj 时已查找但找不到 db/get-user,并指出问题出在第 38 行第 14 列。您将 db 别名为 guestbook.models.db。让我们在guestbook/models/db.clj 中查找get-user 函数。它不在那里!将函数添加到 db.clj 或从 auth.clj 中删除该函数的所有使用。

【讨论】:

非常感谢!我调整了代码以确保它与我在教程中的位置相匹配,并且它似乎正在启动 localhost 但现在我在浏览器中得到了这个! java.lang.ClassCastException clojure.lang.Var$Unbound 无法转换为 java.util.concurrent.Future ...(未知来源) ring.adapter.jetty.proxy$org.eclipse.jetty.server.handler.AbstractHandler$0 .handle ... 仅凭这么多信息无法解决问题。 我在图书网站上转发了我的问题,并扩展了堆栈跟踪:forums.pragprog.com/forums/308/topics/12523> 链接对我来说已经失效了,无论如何你都想打开一个新问题,因为这个问题已经解决并且你的新问题不相关。 好的,很酷。这是我在这里的第一个问题,所以我还在学习规则。非常感谢您的帮助!

以上是关于“使用 Clojure 进行 Web 开发”:没有这样的变量:db/get-user的主要内容,如果未能解决你的问题,请参考以下文章

linux下tomcat启动没有日志,没有进程,没有报错,没有监听端口

程序崩溃, 没有任何提示!没有异常!没有任何错误日志。。。

为啥 HKSample 数组对于一个没有限制、没有谓词、没有锚点的 HKAnchoredObjectQuery 总是有 1 个值?

为啥 HKSample 数组对于一个没有限制、没有谓词、没有锚点的 HKAnchoredObjectQuery 总是有 1 个值?

Eclipse C++ 问题:没有二进制文件(但没有编译器错误),没有类(但在类层次结构中没有条目)

PLSQL提示没有登录ORA-01012:没有登录