How to get sync gateway document using query view?

Hi,
I created view (design name- dev_gettest and view name -category). This is my function code

function (doc, meta) {
   if(doc.type=="category"){
      emit(doc.name,null);
     }
 }

i am getting result from cb server

http://127.0.0.1:8092/test/_design/dev_gettest /_view/_category?limit=6&stale=false&connection_timeout=60000&inclusive_end=true&skip=0&full_set=true

this is my cURL code for sync gateway :

curl -X GET "http://localhost:4985/test/_design/dev_gettest /_view/_category" -H "accept: application/json"

output : {“total_rows”:0,“rows”:[],“Collator”:{}}

curl --cookie "SyncGatewaySession=0b7acd58f576497591ac394ae7be5f0cf24926de" -X GET "http://localhost:4985/test/_design/dev_gettest /_view/_category"

output : {“total_rows”:0,“rows”:[],“Collator”:{}}

i added the following entry to my database config:

 "adminInterface": "localhost:4985",  "interface": "0.0.0.0:4984","databases": {
"test": {
"unsupported": {
      "user_views": {
        "enabled":true
      }
    },  "users": {....}  }

I am unable to get document by cURL!
Couchbase server - 5.0
Couchbase SyncGatway - 1.5.1

Are you sure that documents matching the view are synced through the SGW ? What does your SGW config file look like ?

1 Like

Thanks for reply, The sync gateway view doesn’t created correctly. i correct it now. i am getting result from following cURL command

curl -X GET "http://localhost:4985/test/_design/dev_gettest /_view/_category" -H "accept: application/json" 
 curl --cookie "SyncGatewaySession=8e99deadd4e47ec5cca660965530df1d53559e9a" -X GET "http://localhost:4984/test/_design/dev_gettest /_view/_category" -H "accept: application/json"

The issue is unable to get result by Guzzle http client. i am getting this message.

GuzzleHttp\Exception\ClientException: Client error: GET http://localhost:4984/test/_design/dev_gettest /_view/_category resulted in a 401 Unauthorized
response:{“error”:“Unauthorized”,“reason”:“Login required”}

php code

$jar = new \GuzzleHttp\Cookie\CookieJar;
$cookieJar = $jar->fromArray(['SyncGatewaySession' => $sync_session],'127.0.0.1');
$client = new \GuzzleHttp\Client ([ 'cookies' => $cookieJar, 'allow_redirects' => true,                       'debug' => false ]);   $response = $client->get("http://localhost:4984/test/_design/dev_gettest /_view/_category");

{
“adminInterface”: “127.0.0.1:4985”,
“interface”: “0.0.0.0:4984”,
“log”: ["*"],
“databases”: {
“test”: {
“server”: “http://admin:pass@127.0.0.1:8091”,
“bucket”: “test”,
“users”: {
“user1”: {“password”: “pass”, “admin_channels”: [“user1”]},
“mod”: {“password”: “pass”, “admin_roles”: [“moderator”]},
“admin”: {“password”: “pass”, “admin_roles”: [“admin”]}
},
“roles”: {
“moderator”: {},
“admin”: {}
},
“sync”: `function(doc, oldDoc){
if (!isDelete()) {
if (doc.type == “category”) {
channel(doc.store_name);
access(doc.owner, doc.store_name);
}
}
function isDelete() {
return (doc._deleted == true);
}

 }`,
  "unsupported": {"user_views": {"enabled":true}}
}

},
“logging”: {
“default”: {
“logFilePath”: “E:/sync_gateway_log.log”,
“logKeys”: ["*"],
“logLevel”: “debug”,
“rotation”: {
“maxsize”: 1,
“maxage”: 30,
“maxbackups”: 2,
“localtime”: true
}
}
}
}

Thank you.

Well, creation of views via the REST API is an admin level operation. We never officially supported the “user_views” - that’s why it is not documented under PUBLIC API. That said, AFAIK, you could probably still create the view (by configuring the “unsupported” property as you have specified above) but it’s likely that would probably get no results when you query .

Your 401 has to do with with incorrect credentials . Curious - are you able to run any other Public API (like fetching docs for instance)

1 Like

Thank you priya.rajagopal, I am getting a results. I cleared all sessions and recreated session with valid credentials. before getting results from view i checked whether the session exits or not. Finally i am getting results from design document without any issue.

Thank q.