我正在尝试从Clojurescript / Reagent中的API调用中呈现JSON数据。当我使用js/alert
时,我会看到我期望的json:["Sue" "Bob"]
(defn- call-api [endpoint]
(go
(let [response (<! (http/get endpoint))]
(:names (:body response)))))
;; -------------------------
;; Views
(defn home-page []
[:div (call-api "/api/names")])
这就是我引用库的方式(如果那里存在问题)。
(ns myapp.core
(:require [reagent.core :as reagent :refer [atom]]
[reagent.session :as session]
[cljs-http.client :as http]
[cljs.core.async :refer [<! >!]]
[secretary.core :as secretary :include-macros true]
[accountant.core :as accountant])
(:require-macros [cljs.core.async.macros :refer [go]]))
但是当我将它记录到控制台时,我得到的长哈希看起来与API响应完全不同。浏览器呈现&#34; 00000000000120&#34;。
答案 0 :(得分:3)
当你致电call-api
时,它会返回一个go块。您可以改为在鼠标中更新返回值,而不是直接在试剂函数中尝试使用该块。
(def app-state (atom)) ;; ratom
(defn- call-api [endpoint]
(go
(let [response (<! (http/get endpoint))]
(reset! app-state (:names (:body response))))))
(defn home-page []
[:div @app-state])
(defn main []
(call-api))