re-frame:订阅未按预期工作

re-frame: subscription not working as expected

从 re-frame 开始,有一个(可能是非常基本的)困惑点。

我订阅了:

(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))))))

此 Form-2 组件使用:

(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..."])])))

我的app-db有有效数据,{:votes ...是地图的向量,每个地图都有一个:id参数,{:current-vote是一个数字。

我知道 get-vote-by-id 有效:

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

如果我将 (get-vote-by-id votes cv) 中的 cv 替换为常量:(get-vote-by-id votes 2),那么它就可以工作。但除此之外,(get-vote-by-id ...) 正在计算一个空向量 [].

感谢任何帮助,并提前致谢!

这表明 get-vote-by-id 传递了正确的值(投票向量和 id 数字):

经过一番调试,我们发现问题出在更上游的地方。根本原因是 id 被设置为字符串。将 id 强制转换为整数解决了问题。