Search code examples
clojurescriptre-frame

re-frame: subscription not working as expected


Starting out with re-frame and have a (probably very basic) point of confusion.

I have a subscription:

(defn get-vote-by-id
  [votes id]
  (filterv #(= (:id %) id) votes))

(register-sub
 :cvs
 (fn cvs-sub [db [_]]
   (make-reaction
    (fn cvs-sub-reaction []
      (let [cv (get-in @db [:current-vote])
            votes (get-in @db [:votes])]
        ;; why is this evaluating to []?
        (get-vote-by-id votes cv))))))

used by this Form-2 component:

(defn vote-page []
  (let [ready? (subscribe [:current-vote-initialised?])
        current-vote-id (subscribe [:current-vote-id-sub])
        cv (subscribe [:cvs])]
    (fn vote-page-renderer []
      [:div
       [:h2 "Vote"]
       (if @ready?
         (do
           [:div
            [:div (str "cv: " @cv)]
            [:div (str "Current Vote id: " @current-vote-id)]])
         [:div "Initialising..."])])))

My app-db has valid data, {:votes ... is an vector of maps, each map having an :id param, {:current-vote is a number.

I know that get-vote-by-id works:

(get-vote-by-id [{:id 1 :data "hi"} {:id 2 :data "there"}] 2)
[{:id 2, :data "there"}]

And if I substitute cv in (get-vote-by-id votes cv) with a constant: (get-vote-by-id votes 2), then it works. But otherwise, (get-vote-by-id ...) is evaluating to an empty vector [].

Any help appreciated, and thanks in advance!

This shows that get-vote-by-id is passed correct values (vector for votes and number for id):

get-vote-by-id


Solution

  • After some debugging, we found that the problem was further upstream. The root cause was that id was being set to a string. Coercing id to an integer solved the problem.