Unable to create view in sync gateway

I successfully create view in cb server. i try to create view in sync gate way using REST API

But i am getting error. this is my map function

function (doc, meta) {  if(doc.type  &&  doc.type!= null && doc.type=='item'){ 
emit(doc.item_category.catName,doc);   }}

error

{“error”:“Bad Request”,“reason”:“Bad JSON”}‘doc.type!’ is not recognized as an i
nternal or external command,
operable program or batch file.

You’re most likely having a problem with the JSON formatting in your REST call. What are you using to make the call? Can you post either the exact command or a network dump of the request?

1 Like

Thank you, this is my code

curl -X PUT "http://localhost:4985/todo/_design/getdocument" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"_rev\": \"string\", \"views\": { \"getDoc\": { \"map\": \"function (doc, meta) { if(doc.type && doc.type!= null && doc.type=='item'){ emit(doc.item_category.catName,doc); }}\", \"reduce\": \"string\" } }}"

error :

“error”:“Bad Request”,“reason”:“Bad JSON”}‘doc.type!’ is not recognized as an i
nternal or external command,
operable program or batch file.

I try to get documents from this function

function (doc, meta) {  if(doc.type  &&  doc.type!= null && doc.type=='item'){ emit(doc.item_category.catName,doc);   }}

Looks like a problem with escaping your JSON body… Try this

curl -X PUT \
  http://localhost:4985/travel-sample/_design/getdocument\
  -H 'Accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{"_rev":"123","views": {"getDoc": {"map": "function (doc, meta) {  if(doc.type && doc.type!= null && doc.type=='item'){ emit(doc.item_category.catName,doc);}}"}}}'

1 Like

Thank you, I am getting same error. i am able to create design view with following function

function (doc, meta) {  if(doc.type=='item'){ emit(doc.item_category.catName,doc);   }}

But when i try to create if(doc.type && doc.type!= null && doc.type==‘item’) condition, i am getting error.

I created design view in couchbase server console. it is working fine.

function(doc,meta) {
	                    var sync = doc._sync;
	                    if (sync === undefined || meta.id.substring(0,6) == "_sync:")
	                      return;
	                    if ((sync.flags & 1) || sync.deleted)
	                      return;
	                    var channels = [];
	                    var channelMap = sync.channels;
						if (channelMap) {
							for (var name in channelMap) {
								removed = channelMap[name];
								if (!removed)
									channels.push(name);
							}
						}
	                    delete doc._sync;
	                    meta.rev = sync.rev;
	                    meta.channels = channels;

	                    var _emit = emit;
	                    (function(){
		                    var emit = function(key,value) {
		                    	_emit(key,[channels, value]);
		                    };
							(function (doc, meta) { if(doc.type && doc.type!= null && doc.type=='category') 		emit(doc.cat_name,doc); }) (doc, meta);
						}());
						doc._sync = sync;
					}

I’ve tested and @priya.rajagopal’s solution seems to work on a Mac running bash. What OS/shell are you using? The fact that the error is reported on doc.type! makes me suspect the quoting isn’t right for your platform, and the ! is getting interpreted as a special character.

Thank you @hod.greeley
i am using windows. it is getting error with special character. still i am unable to create view using cURL (with multiple condition ). i created view via couchbase console .