clojureclojurescripttransit

ClojureScript: parsing a Transit response


I started learning ClojureScript this week and I stuck parsing a Transit response, I have this function:

(defn handler [response]
   (let [comment (:comment response)
         created_at (:created_at response)
         last_name (:last_name response)
         _ (.log js/console (str ">>> COMMENT >>>>> " comment))
        comments_div (.getElementById js/document "comments")]
      (.append comments_div comment)
      (.log js/console (str "Handler response: " response))))

And the console shows:

enter image description here

So, "response" looks fine but I can't get the content from the "response" map (I think is a map) using:

 comment (:comment response) or comment (get response :comment)

The headers say that the response is an "application/transit+json" kind. I tried:

     (ns blog.core
        (:require [cognitect.transit :as t])) 

       (def r (t/reader :json))

      let [parsed (t/read r response).... <--- inside the let block

but no luck so far. Need I to parse the var "response"?


Solution

  • This works fine:

     (ns blog.core
       (:require [domina :as dom]
            [ajax.core :refer [GET POST DELETE]]
            [cognitect.transit :as t]
            [bide.core :as r]))
    
    (def r (t/reader :json))
    
    (defn handler [response]
      (let [parsed (t/read r response)
            _  (.log js/console (str ">>> PARSED >>>>> " (type parsed) ">>>>" parsed))
        comment      (get parsed "comment")
        .... rest of the code...