0

Getting clojure.core$count cannot be cast to java.lang.Number error for defined variable count

(defroutes jobs-routes
      (context "/jobs" []
        (POST "/jobValidation" [name status req]  (insert-job jobName jobStatus req)))

In this method I've define a variable count to store the value of total count from the query

(defn insert-job [jobName jobStatus req]
   (let [count (db/insert-job {:job-name name :status status})])
    (when (> count 0) (println (format "true" )))
    (insert-job req) ; 
)

Query: db/insert-job

-- :name insert-jobWithValidation :? :*
SELECT count(*)  FROM job_history WHERE job_name = :job-name and status = :status

Problem I want to check that whether If I'm getting out value greater than 0 so I can print something any if count is any positive value then I can perform something new

2 Answers 2

3

Your let block has no body, you're closing it right after definitions vector:

(let [count (db/insert-job {:job-name name :status status})])

Note that you are shadowing core function count here.

So when you call this line: (when (> count 0) (println (format "true" ))), the value of count is count function:

count
=> #object[clojure.core$count 0x69c6a700 "clojure.core$count@69c6a700"]
(> count 0)
Execution error (ClassCastException)
class clojure.core$count cannot be cast to class java.lang.Number

There are some other errors and style inaccuracies:

  • the names of your variables don't match (jobName jobStatus vs name status)
  • you're calling (insert-job req) with only one argument, but this function has three
  • (> ... 0) is pos?
  • (format "true") is just "true"

Possible fix (maybe you will still have to modify it somehow):

(defn insert-job [job-name job-status req]
  (let [my-count (db/insert-job {:job-name job-name :status job-status})]
    (if (pos? my-count)
      (println "true")
      (insert-job job-name job-status req)))) 
Sign up to request clarification or add additional context in comments.

1 Comment

tried your solution but getting * clojure.lang.PersistentArrayMap cannot be cast to java.lang.Number* at pos?
1

that is probably because of your hugsql query header:

:? :* - returns the rowset, which is not a number. maybe :? :1 would be more suitable here. Anyway, the query doesn't return a count(*), it rather returns a hash-map (in case of :1) or a list of one hashmap (in case of :*), you would still need to retrieve the value from there.

Something like that (untested):

-- :name insert-jobWithValidation :? :1
SELECT count(*) as amount  FROM job_history WHERE job_name = :job-name and status = :status
(defn insert-job [name status req]
  (let [res (db/insert-job {:job-name name :status status})]
    (when (-> res :amount pos?) 
      (println true))
    (insert-job req)))

UPD: omg, let with no body, indeed, @Martin Půda, thanks )

i would say, what you want is something like that:

-- :name count-jobs-by-name-and-status :? :1
SELECT count(*) as amount  FROM job_history WHERE job_name = :job-name and status = :status
(defn insert-job-if-not-exists [name status req]
  (if (->> {:job-name name :status status}
           db/count-jobs-by-name-and-status
           :amount
           pos?)
    (println "job already exists")
    (insert-job req)))

10 Comments

getting java.lang.NullPointerException at pos?
could you please try to log what you got in res? i bet that would clarify the whole thing
this is stored in res : {:count 1}
this is exact count for which I want the job with particular name & status
this is weird. have you updated your sql with alias to 'amount' as shown above?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.