Hunchentoot 处理程序更改另一个函数的定义

Hunchentoot handler changes the definition of another function

我写了一些代码来管理 postgresql 数据库,它正在控制台上运行。现在我想通过 hunchentoot 把它放在我的内部网络上。

我使用clsql,并将数据库代码打包为:

(defpackage :my-database
  (:use :common-lisp)
  (:documentation "several lines...")
  (:export almost-all-functions...))

(in-package #:my-database)

(defun new-patient (name gender height weight)
  "adds new patient to the db. all parameters are of type string."
  (let* ((height (parse-integer height))
         (weight (parse-integer weight))
         (corrected-weight (calculate-corrected-weight height weight gender)))
    (clsql:insert-records :into 'patients
                          :attributes '(height weight corrected-weight name gender patient-at-ward)
                      :values (list height weight corrected-weight name gender t))))

我导入了我的数据库包,当我使用这个处理程序时:

(define-easy-handler (debug :uri "/debug")
    (name gender height weight)
  (let ((ad (substitute #\space #\+ (string-trim " " ad))))
    (progn (format nil "Name:~A, Gender:~A, Height:~A, Weight:~A" name gender height weight)
           (redirect "/success"))))

显示html就好了。但是这个处理程序:

(define-easy-handler (new-patient :uri "/new-patient")
    (name gender height weight)
  (let ((name (substitute #\space #\+ (string-trim " " name))))
    (progn (my-database:new-patient name gender height weight)
           (redirect "/success"))))

抛出错误:

Incorrect keyword arguments in ("Frank Sinatra" "male" "150" "55")

我注意到当我在 REPL 中键入 (my-database:new-patient ) 时,emacs 命令迷你缓冲区显示 (my-database:new-patient &key name gender height weight)&key 部分在我看来很可疑。因此,我切换到我的数据库文件,做了 slime-eval-last-expression-in-repl,它更正了 emacs 命令迷你缓冲区显示。

但是这次,我得到了以下错误:

Too few arguments in call to #<Compiled-function MY-DATABASE:NEW-PATIENT #x15870D76>:
0 arguments provided, at least 4 required.

重新评估 hunchentoot 处理程序导致 emacs 命令迷你缓冲区再次显示 &key。最后,我将处理程序代码更改为 (my-database:new-patient :name name :gender gender :height height :weight weight),这会引发此错误:

8 arguments were provided, but at most 4 are accepted by the current global
definition of NEW-PATIENT

可能是什么原因?

原因是 define-easy-handler 的语义:

The resulting handler will be a Lisp function with the name name and keyword parameters named by the var symbols.

(根据 hunchentoot 文档:http://weitz.de/hunchentoot/),因此您应该为处理程序使用不同的名称,例如:

(define-easy-handler (do-new-patient :uri "/new-patient")
   (name gender height weight)
(let ((name (substitute #\space #\+ (string-trim " " name))))
  (progn (my-database:new-patient name gender height weight)
         (redirect "/success"))))