0
votes

I am trying to add documents to couchbase server(admin UI) and then trying to retrieve it using couchbase Lite via sync gateway but unable to do so. What I am trying to achieve is I already have a lot of data in couchbase server now I want my mobile app to use it and because that data was not added using sync gateway I want to achieve something like I added data using web now I want my couchbase lite to connect to that couchbase server and retrieve data. Is there any way to do it? or only data that has been added using sync gateway can be retrieved?

EDIT 1 Added Source Codes

Below is the android app code

package com.couchbase.examples.couchbaseevents;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.util.Log;

import com.couchbase.lite.CouchbaseLiteException;
import com.couchbase.lite.Database;
import com.couchbase.lite.Document;
import com.couchbase.lite.Manager;
import com.couchbase.lite.android.AndroidContext;
import com.couchbase.lite.replicator.Replication;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    public static final String DB_NAME = "couchbaseevents";
    final String TAG = "CouchbaseEvents";
    Database database = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d(TAG, "Begin Couchbase Events App");
    Manager manager = null;
    try {
        manager = new Manager(new AndroidContext(this), Manager.DEFAULT_OPTIONS);
        database = manager.getDatabase(DB_NAME);
    } catch (Exception e) {
        Log.d(TAG, "Error getting database", e);
        return;
    }
    /*try {
        database.delete();
    } catch (Exception e) {
        Log.e(TAG, "Cannot delete database", e);
        return;
    }*/
    try {
        startReplications();
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
    Document retrievedDocument = database.getDocument("123");
// display the retrieved document
    Log.d(TAG, "retrievedDocument=" + String.valueOf(retrievedDocument.getProperties()));
    Log.d(TAG, "End Couchbase Events App");
    }
    private URL createSyncURL(boolean isEncrypted){
    URL syncURL = null;
    String host = "http://172.16.25.100";
    String port = "4986";
    String dbName = "sync_gateway";
    try {
        //syncURL = new URL("http://127.0.0.1   :4986/sync_gateway");
        syncURL = new URL(host + ":" + port + "/" + dbName);
    } catch (Exception me) {
        me.printStackTrace();
    }
    Log.d(syncURL.toString(),"URL");
    return syncURL;
    }
    private void startReplications() throws CouchbaseLiteException {
    Replication pull = database.createPullReplication(this.createSyncURL(false));
    Replication push = database.createPushReplication(this.createSyncURL(false));
    pull.setContinuous(true);
    push.setContinuous(true);
    pull.start();
    push.start();
    if(!push.isRunning()){
        Log.d(TAG, "MyBad");
    }
    /*if(!push.isRunning()) {
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getMessage());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getCause());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().getStackTrace());
        Log.d(TAG, "Replication is not running due to " +push.getLastError().toString());
    }*/
    }

}

"123" is the document id of document I created in CouchBase server using admin UI

As you can see I first deleted the database( commented part) to make sure there is no document in database and then ran the above replication code.

Below is the sync gateway config file

{
    "log":["CRUD+", "REST+", "Changes+", "Attach+"],
    "interface":":4986",
   "adminInterface":":14985",
    "databases": {
    "sync_gateway": {
        "server":"http://172.16.25.100:8091",
        "bucket":"sync_gateway",
        "sync":`
            function (doc) {
            channel (doc.channels);
        }`,
        "users": {
            "GUEST": {
                "disabled": false,
                "admin_channels": ["*"]
            }
        }
    }
    }
}

I also want to ask is there any UI or command line to access CBL. I am currently using CBL in android studio so I dont know how to access its UI or command line

Just for the information, I am able to push data from CBL to CouchBase server

1
We'll need your sync gateway configuration and the code in CB lite by which you define, configure, and start the pull and push synchronizations, to give any advice. - TAM
Edited my post. Please Check again - Legendary_Hunter
Doesn't look bad at first sight. However, I have no experience with performing synchronization for a guest user with channels = * only. Appears rather unsecure, as e.g. any of us reading this would now have access to your database. I always disable the guest and work with regular, logged-in user. But that may not be the source of your problem. What happens if you step through this code with the debugger? Is sync not working only in one direction or in both? - TAM
Is there any way I can look int couchbase lite to check whether documents are coming? (Except using the above android api) - Legendary_Hunter
When I debug I get pull = Replication{http://172.16.25.100:4986/sync_gateway, pull} and push = Replication{http://172.16.25.100:4986/sync_gateway, push} - Legendary_Hunter

1 Answers

0
votes

We can solve the above issue by shadowing. If I want to sync my data from the already made bucket to sync gateway bucket shadowing is required. More about Shadowing here