ShwetaJ
ShwetaJ

Reputation: 519

Can Cloudant list/show functions return objects and arrays(any JSON)?

As per the API documentation for Cloudant: Show function can be used to render a document in a different format or extract only some information from a larger document. Same is the case for a list function, the only difference is that it applies on a set of documents. I created a design document with a show function as follows:

{ "shows": { "showDemo":"function(doc,req){return {'body': doc, 'headers':{'Content-Type':'application/json'}}}" } }

When I use this function, _design/showFunc/_show/showDemo/doc1, I get the following error:

{ "error": "unknown_error", "reason": "badarg", "ref": 1793182837 }

I have observed the same error when the show function returns an array. However, no error is given when HTML,Text, XML is returned. Can we say that list/show functions can only return data in a format other than JSON? This example shows the "Accept" header for req object request Object.

Upvotes: 0

Views: 274

Answers (1)

Lorna Mitchell
Lorna Mitchell

Reputation: 1986

What's happening here is that the show function needs to return a response object. From the docs (see http://docs.couchdb.org/en/2.1.0/json-structure.html#response-object) the body field needs to be a string, so you can return whatever you like but it needs to be stringified or otherwise turned into a format that can be sent as HTTP.

If you want to send JSON then doing JSON.Stringify(doc) as the value for body should do what you expect.

Upvotes: 1

Related Questions