This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-03-13
Channels
- # admin-announcements (1)
- # beginners (53)
- # boot (355)
- # braid-chat (4)
- # cider (15)
- # cljsjs (3)
- # clojure (88)
- # clojure-austin (1)
- # clojure-new-zealand (4)
- # clojure-russia (46)
- # clojure-uk (7)
- # clojurescript (61)
- # component (2)
- # cursive (3)
- # data-science (5)
- # datomic (6)
- # dirac (49)
- # editors (8)
- # emacs (15)
- # funcool (15)
- # jobs (1)
- # ldnclj (10)
- # melbourne (1)
- # off-topic (14)
- # om (61)
- # om-next (2)
- # onyx (32)
- # re-frame (28)
- # reagent (19)
- # test200 (1)
- # vim (3)
I'm getting back
{:id 2, :title "foo"}{:id 3, :title "bar"}
as the body of a response, but it's being recorded as a string and not json
The request is responding with the following route at handler.clj:
(GET "/titles" [] {:body (all-titles) :format :json})
and is called with the following cljs-ajax request:
(GET "/titles" {:handler titles-handler :format :json}))
Hello clojurians. I'm doing clojure koans at the moment. Can someone tell me what is wrong here:
(defn recursive-reverse [coll]
(if (empty? coll)
coll
(conj (recursive-reverse (rest coll)) (first coll))))
(recursive-reverse `(1 2 3)) # => (1 2 3)
@mkaschenko: if you conj
onto a list, it adds to the front.
hello @bwstearns, I’ve tried to find the :format
keyword but couldn’t find any usage in compojure or ring, do you know what library uses it?
@maxov I saw here: http://yogthos.net/posts/2013-04-09-Introducing-cljs-ajax.html I didn't see it in the compojure side at all, but I just tried it because I'm out of ideas and it didn't error.
ok, :format is used by cljs-ajax on the client side for the format of the body, but it isn’t used anywhere in your backend stack
Is the fact that the keys are clojure keywords in the message body possibly the reason?
I’d recommend wrapping your routes with a library that handles json, probably https://github.com/ring-clojure/ring-json is best
My middleware looks like:
(defn wrap-middleware [handler]
(-> handler
(wrap-defaults api-defaults)
wrap-exceptions
(json-middleware/wrap-json-body {:keywords? true})
json-middleware/wrap-json-response
wrap-reload))
I’m not sure why it’s still responding with EDN — it seems like you’re using ring-json correctly
I've been putting off going and learning about EDN until after I figured this out..... I'm a damned idiot haha.
yes. edn is a data transport format very similar to json that’s basically just clojure data structures
ok, so the problem is then likely to be server side somewhere because the data isn't being encoded in JSON?
could you elaborate what you mean by ‘recorded as a string and not json’ on the front end
So, the call looks like this:
(defn pieces-handler [pcs]
(swap! app-state assoc :all-pieces pcs))
(defn fetch-pieces []
(GET "/art/pieces" {:handler pieces-handler :format :json}))
the js->clj bit was kinda like the :format on the server in that it wasn't there most of the time it was kinda of a shot in the dark
Huh, something I changed on the backend as a result of our conversation apparently flipped the right bit because it's not coming over the wire as EDN anymore
ok so now the problem must be that I'm not converting the received JSON into a cljs map right?
oh, sorry I got sidetracked. There is a component that makes a list from the pieces in :all-pieces
. Right now there should be 5, and its producing 143 with nils for fields because apparently :all-pieces
is a string so it's iterating over each letter
stagistry.core=> (:all-pieces @app-state)
"[{\"id\":2,\"title\":\"foo\",\"description\":\"bar\",\"price\":5,\"artists_id\":null},{\"id\":3,\"title\":\"foo\",\"description\":\"bar\",\"price\":5,\"artists_id\":null},{\"id\":4,\"title\":\"The Silence of the Lambda\",\"description\":\"Something something java beans and a nice chianti\",\"price\":5000,\"artists_id\":null},{\"id\":5,\"title\":\"The Silence of the Lambda\",\"description\":\"Something something java beans and a nice chianti\",\"price\":6000,\"artists_id\":null}]"
I tried adding json-response-format to the GET call:
(GET "/art/pieces" {:handler pieces-handler :json-response-format {:keywords? true}}))
(GET "/art/pieces" {:handler pieces-handler :json-response-format {:keywords? true :raw false} ::response-format :json})
Oh wow. that worked. the only difference from perfect is that the keys are strings not keywords, which is probably what I missed all those errors ago and how I ended up with a string instead of a map.
Sorry for the somewhat spazzy description of the problem, it's one of those things where I had pretty much run out of ideas and so there were likely several places where I had started to actively impair my odds of success as opposed to get closer to the answer.
@codonnell: thank you
i had a few middleware functions in a core.clj file, and decided to move them to a middleware.clj and important them into core.clj with a :refer statement
one of the functions called wrap-db
uses the value of db
which is just a def
statement in core.clj
i’m wondering how i can keep that middleware function out of my core.clj but still ref that db value from core
yes it usually is, most often you have to get back to the drawing table and reconsider the bigger picture
@chadhs: @lmergen I had good success with: https://github.com/timmc/nephila when this happened to me