0

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"?

aarkerio
  • 2,183
  • 2
  • 20
  • 34
  • 1
    If the response is just a string, `:comment` will return `nil`, which won't show up in a string. Try `(type response)` to see what's going on. – Carcigenicate Mar 03 '18 at 22:22

2 Answers2

2

Since it is not working like a map, it probably is a string. Try checking the type of response. with

(println (type response))

If it is a string then :

(ns example
  (:require [clojure.data.json :as json]))
  (console.log ((json/read-str response) :comment))
pankajdoharey
  • 1,562
  • 19
  • 30
  • 2
    Thanks a lot, you put me in the right track. Unfortunately, clojure.data.json is exclusively for Clojure and won't work inside ClojureScript. However: "[cognitect.transit :as t]" do the trick. – aarkerio Mar 04 '18 at 22:34
  • 1
    Ohh thanks I didnt actually look at the API, just typed from the top of my head. – pankajdoharey Mar 04 '18 at 23:11
1

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... 
aarkerio
  • 2,183
  • 2
  • 20
  • 34