Problems in replicating data from server to CBL local database


#1

Hi guys,
Can I have a quick solution for this,
In the CouchBase server, I have these three types of documents

And in the sync gateway, I’ve created a test user (username=firstUser, password=123) using “CURL” command. And the user has created successfully.

_sync:user:firstUser

{
“name”: “firstUser”,
“all_channels”: {
“!”: 1,
“public”: 4,
“user-firstUser”: 4
},
“sequence”: 5,
“passwordhash_bcrypt”: “JDJhJDEwJDVMLmppN0FVcU9OdWhqUnRkYWRHcnVRVy91WkhQc2E2UGtFclMuRUlxVnR4bzhmYjBtNVZD”,
“rolesSince”: {}
}

And in the Client app, I used basic HTTP auth with username and password of test user,


Authenticator auth = AuthenticatorFactory.createBasicAuthenticator(“firstUser”, “123”);
pushReplication.setAuthenticator(auth);
pullReplication.setAuthenticator(auth);


Initially, client app had no data in local database, what I wanted to do was to pull above 3 documents to the local database.

The Profile document is private to the user, so I needed to pull that user specific profile document only. (there will be many Profile type documents for different users)

And other two documents (Person and Group) are public, that is any user can pull them to their local db.

So I used the following SGW configuration, I’ve disabled the guest access.


{
  "adminInterface": "127.0.0.1:4985",
  "interface": "0.0.0.0:4984",
  "log":["REST"],
  "databases":{
     "eegbase":{
         "server":"http://localhost:8091",
         "sync":`function(doc) {
             if(doc.type="Group"){
  	      channel("public");
             }
	     if(doc.type="Person"){
  	      channel("public");
             }
	     if(doc.type="Profile"){
              requireUser(doc.userName);
	      channel("user-"+doc.userName);
	      access(doc.userName, ["user-"+doc.userName,"public"]);
             }        	 
         }',
         "users":{
	     "GUEST":{"disabled":true,"admin_channels":["*"]}
	 },
         "shadow": {
             "server": "http://localhost:8091",
             "bucket": "eegbase_backend"
         }
     }
  }
}


Problem is, I only receive the “Profile” document. Other two documents are not replicated to the local database. Why is that? Am I using a wrong configurations? (It worked with no user authentication and guest access)

My terminal window shows following commands


13:34:51.750691 HTTP: #001: GET /eegbase/_session (as firstUser)
13:34:51.761001 HTTP: #002: GET /eegbase/_session (as firstUser)
13:34:51.780034 HTTP: #003: GET /eegbase/_local/fcddf639dd77f7af379546c8ca43d8700af58292 (as firstUser)
13:34:51.780274 HTTP: #003: --> 404 missing (0.7 ms)
13:34:51.780676 HTTP: #004: GET /eegbase/_local/9aee9f892cb6afc2cd71784244f6daa8c00083bf (as firstUser)
13:34:51.780797 HTTP: #004: --> 404 missing (0.6 ms)
13:34:51.796580 HTTP: #005: GET /eegbase/_session (as firstUser)
13:34:51.803773 HTTP: #006: GET /eegbase/_local/fcddf639dd77f7af379546c8ca43d8700af58292 (as firstUser)
13:34:51.803892 HTTP: #006: --> 404 missing (0.4 ms)
13:34:51.857694 HTTP: #007: POST /eegbase/_changes (as firstUser)

Thanks
-Isuru


#2

@cyclops15

Could you run the following curl commands against your Sync Gateway and post the results:

curl -X GET http://firstUser:123@your-server:4984/yourdb/_changes

This should emulate what your mobile client is calling and will show the filtered list of revisions for that user.

To see all the changes against the DB for all users you can run the same command on the ADMIN REST API, this will show the full unfiltered list of revisions in the db.

curl -X GET http://your-server:4985/yourdb/_changes

It might also be useful to post a list of all the docs in the DB, run the following command from the ADMIN API:

curl -X GET http://your-server:4985/yourdb/_all_docs?incude_docs=true

Andy


#3

Hi Andy,

Thanks for responding, I ran the first CURL command as followes;

cyclops@cyclops-S550-PR25KN:~$ curl -X GET http://firstUser:123@localhost:4984/eegbase/_changes

And the results were…

{“results”:[
{“seq”:4,“id”:“eeg-prof001-ic”,“changes”:[{“rev”:“1-4b203f7f78f0286954aa43c320588fd4”}]}
,{“seq”:5,“id”:"_user/firstUser",“changes”:[]}
],
“last_seq”:“5”}

And for the command,

curl -X GET http://localhost:4985/eegbase/_all_docs?incude_docs=true

I got,

{“rows”:[
{“key”:“eeg-person001-ic”,“id”:“eeg-person001-ic”,“value”:{“rev”:“1-d51ee363ed12e7f815553109ddd9b331”}}
,{“key”:“eeg-prof001-ic”,“id”:“eeg-prof001-ic”,“value”:{“rev”:“1-4b203f7f78f0286954aa43c320588fd4”}}
,{“key”:“eeg-resgrp001-ic”,“id”:“eeg-resgrp001-ic”,“value”:{“rev”:“1-e4d5e3a0d413dafbea5ad46224d90d19”}}
],
“total_rows”:3,“update_seq”:10}


#4
             if(doc.type="Group"){

That’s incorrect. JavaScript uses == for comparison. What you’re doing is assigning "Group" to doc.type, and then testing the result, which will always succeed.

You have two or three other places where you’ve made the same mistake.


#5

Sorry for the dumb mistake. I corrected it, but the result is same. Only receive “Profile” document…:frowning:

Inside the Profile document at Sync Gateway bucket…,

“channels”: [
[
“user-firstUser”
]
]
},
“channels”: {
“user-firstUser”: null
},
“access”: {
“firstUser”: {
“public”: 4,
“user-firstUser”: 4
}
},…

but in the “Group” and “Person” documents has this…

“channels”: [
[
“user-undefined”
]
]
},
“channels”: {
“user-undefined”: null
},

According to me, these two types are failed to assigned to channels, but the problem is why…


#6

@cyclops15

After changing the sync function, you will either need to push new revisions to get the new mapping, or you will need to run a _resync using a URL like:

curl -X POST http://your-server:4985/yourdb/_resync

P.S. In my ADMIN API example previously there was a typo, “?incude_docs=true”, should have been “?include_docs=true”

Andy


#7

Thank you Andy,

It is strange, but I played with a couple of sync function changes and it worked without a problem. I didn’t know about the “resync” thing but I flushed my server database when every time sync function has changed. However it worked finally without a problem.

-Isuru