Hi Shipra,

Erlang views are meant as an expert feature for folks who are proficient in 
CouchDB Views and Erlang, at which point you wouldn’t run into these issues, so 
I’ll reiterate the recommendation you got on Slack and IRC at least three or 
four times now to stick with JS views or Mango for now.

* * *

The error you are seeing is a syntax error in your erlang code. I can’t spot it 
right now, but when taking it out of the JSON and running it in the erlang 
console, it works fine:

2> Emit=fun(K, V) -> 1+2 end. % mock Emit just so the next line compiles
#Fun<erl_eval.12.127694169>

3> fun({Doc}) -> Id = couch_util:get_value(<<"_id">>, Doc), Doc_type = 
couch_util:get_value(<<"TEMPLATE">>, Doc),Emit(Id, Doc_type) end.
#Fun<erl_eval.6.127694169>

What this function does, however is not what you want.

1. it extracts the doc._id field and assigns its value to the `Id` variable.

2. it extracts the doc.TEMPLATE field and assigns its value to the `Doc_type` 
variable.
  - I assume this is an error and you want the doc.type field or somesuch, not 
doc.TEMPLATE.

3. it calls Emit with the Id as key and Doc_type as value.
  - this builds index of docs by _id, and their associated TEMPLATE field value,
    which is not what you describe you want.

To fix this:

1. find where the translation from the Erlang-in-JSON to inside the query 
server goes wrong. Turn on CouchDB debug logging for help.
2. review your code for doc.TEMPLATE vs. doc.type
3. fix your Emit call to index by type instead of id.

* * *

The analysis however confirms that you are best served with JS views and/or 
Mango for this task.

Best
Jan
—
> On 30. Jul 2019, at 10:10, Shipra Choudhary <shipra.choudh...@dltlabs.io> 
> wrote:
> 
> *Hi,*
> 
> I am trying to write a view in CouchDb by Erlang whatever the code I have
> written in Design Document it is running successfully but I am not getting
> the required result which I am expecting. That code is giving random result
> of random doctype not for which I am asking.
> 
>  "_id": "_design/erl_views9",
> 
>  "_rev": "18-02fbdaca7ff5a7ef8aa0ec80a1b8f528",
> 
>  "language": "erlang",
> 
>  "views": {
> 
>      "getTemplates": {
> 
>      "map": "fun({Doc}) -> Id = couch_util:get_value(<<\"_id\">>, Doc),
> Doc_type = couch_util:get_value(<<\"TEMPLATE\">>, Doc),Emit(Id, Doc_type)
> end."
> 
>    }
> 
> }
> 
> }
> 
> If I am using If in the code then It is giving error of Bad Query.
> 
> ERROR: Error running query. Reason: ({badmatch,[<<"error">>,
> {1,erl_parse,["syntax error before: ","'end'"]}, {1,erl_parse,["syntax
> error before: ","'end'"]}]})
> [{couch_query_servers,'-start_doc_map/3-fun-0-',2,
> [{file,"src/couch_query_servers.erl"},{line,61}]},
> {lists,foreach,2,[{file,"lists.erl"},{line,1337}]},
> {couch_query_servers,start_doc_map,3,
> [{file,"src/couch_query_servers.erl"},{line,60}]},
> {couch_mrview_updater,start_query_server,1,
> [{file,"src/couch_mrview_updater.erl"},{line,231}]},
> {couch_mrview_updater,map_docs,2,
> [{file,"src/couch_mrview_updater.erl"},{line,184}]}]
> 
> 
> What am I expecting ?
> 
> By passing particular key in doc_type and should get all the views
> according to key value which I will pass in the function.
> 
> 
> 
> 
> 
> 
> *Shipra Chaudhary*
> *Software Engineer*
> 
> *+91 6390167803*
> *shipra.choudh...@dltlabs.io <shipra.choudh...@dltlabs.io>*
> 
>  *Plot No. 5  **J V P Building,**4th Floor,**Madhapur,*
> 
> 
> * Hyderabad, Telangana- 500081, India.*

-- 
Professional Support for Apache CouchDB:
https://neighbourhood.ie/couchdb-support/

Reply via email to