pitalig
pitalig

Reputation: 177

Error when calling a function using route

I've wrote this function to insert an "account" in the database:

(defn create-account [accountnumber]
  (if (= nil (find-by-number accountnumber))
    (jdbc/insert! db-spec :accounts {:accountnumber accountnumber})
    nil
  ))

This tests passes, so the function it's working:

  (testing "create and find-by-number account"
    (account/create-account 10)
    (is (= 10 ((account/find-by-number 10) :accountnumber)))
  )

But when I try to use this route:

(defroutes app-routes
      (GET "/:account" [account] (account/create-account account))
      (route/not-found "Not Found"))

(def app
  (-> app-routes
    rmp/wrap-params
    wrap-json-response
    wrap-json-body))

I got this error:

org.postgresql.util.PSQLException

ERROR: column "accountnumber" is of type integer but expression is of type character varying Hint: You will need to rewrite or cast the expression.

Verifying types, I can see that when I run the test, accountnumber type is java.lang.Long and when it comes from routes, it's type is java.lang.String, I've tried to convert it but got no success.

Upvotes: 0

Views: 72

Answers (1)

Joshua Smith
Joshua Smith

Reputation: 6641

This is because the route is returning a param. Params are strings.

If you change your route to be something like this:

(defroutes app-routes (GET "/:account" [account] (account/create-account (Integer/parseInt account))) (route/not-found "Not Found"))

it should work.

Upvotes: 1

Related Questions