My question is the same as the question posted here in December of 2014:
I am curious though why I actually need to specify the response map in this particular case, since this is typically done for me. I also worry that this solution will simply break something else.
If I look here:
I see:
clojure.lang.ISeq |
| (render [coll _] |
| (-> (response/response coll) |
| (response/content-type "text/html; charset=utf-8"))) |
Which would make me think that compojure knows how to render a seq into a response, and yet when I try to return a vector, I get:
java.lang.IllegalArgumentException: No implementation of method: :render of protocol: #'compojure.response/Renderable found for class: clojure.lang.PersistentVector
I'm facing an odd situation. If I query the database (using the monger library to query MongoDB), and if I get multiple documents, I can return that without a problem, and everything works as I expect. However, if I return just one document, its keys become keys at the top level of the response, rather than being inside of the :body key, and also the :body key is empty, which is frustrating.
So this is ends up being the response:
{:updated-at 2015-06-16T03:46:13.148Z,
:_id 557f9c050cf200d4b66429ed,
:company-images [
{:company-image "/assets/open/images/ar/badge-theater-small.png"}
{:company-image "/assets/open/images/ar/badge-music-small.png"}
],
:company-name The Ethereal Lightness Of Savings,
:executives [
{:contact-name "Mark Stable", :total-sales "157532"}
{:contact-name "Tori Notworth", :total-sales "2235111"}
{:contact-name "Suzie Undid", :total-sales "1250"}
],
:api-version v1,
:headers {Access-Control-Allow-Headers Authorization, X-Requested-With, Content-Type, Origin, Accept,
Access-Control-Allow-Credentials true,
Access-Control-Max-Age 4440,
Access-Control-Allow-Methods PUT, DELETE, POST, GET, OPTIONS, XMODIFY,
Access-Control-Allow-Origin *,
Content-Type application/json},
:token 56a85d1d-8b37-4aaa-b7d7-399b6c330abd,
:status 200,
:transaction-id 24e6a697-fcf3-4c36-b3d0-d544304c0ee3,
:body }
but I want all of this to go inside of body:
:updated-at 2015-06-16T03:46:13.148Z,
:_id 557f9c050cf200d4b66429ed,
:company-images [
{:company-image "/assets/open/images/ar/badge-theater-small.png"}
{:company-image "/assets/open/images/ar/badge-music-small.png"}
],
:company-name The Ethereal Lightness Of Savings,
:executives [
{:contact-name "Mark Stable", :total-sales "157532"}
{:contact-name "Tori Notworth", :total-sales "2235111"}
{:contact-name "Suzie Undid", :total-sales "1250"}
],
:api-version v1,
I tried to force this issue with this bit in my handler:
results (if (vector? results)
results
[results])
But that simply gave me the Exception that you can see above. Why doesn't this work?
This happens when I'm creating a new document via a PUT, but I'm trying to return the document that was just created. Does compojure allow me to return an entity on PUT and POST?
I will, for now, hard-code the response map, including the body key and its contents. But I am curious why, in this one case, I need to.