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

时间:2015-06-14 11:40:22

标签: common-lisp hunchentoot

我写了一些代码来管理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))))

我导入了my-database包,当我使用这个处理程序时:

(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部分对我来说很可疑。因此我切换到my-database文件,执行了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命令minibuffer再次显示&amp;键。最后,我将处理程序代码更改为(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

可能是什么原因?

1 个答案:

答案 0 :(得分:2)

原因是define-easy-handler

的语义
  

生成的处理程序将是一个名为 name 的Lisp函数和由 var 符号命名的关键字参数。

(根据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"))))