Couchbase Lite Ionic App

Hi Everyone,

I am planned to use Couchbase Lite together with my Ionic App using Cordova Plugin.
I am using example Hotel-Lister as my reference.

I am planned to create my own Document and perform a CRUD operation with it. So far, I am trying to create my own Cordova Plugin with no luck. Please take a look at my Github Repo below and kindy advice me, on which part I am doing it wrongly.

My Cordova Plugin
My Ionic App

All help is really appreciate.
Thank you and regards,
Amer

Can you describe the issue?

We really don’t have the time or resources to do code reviews for forum users. You’ll need to describe the specific problem and show the code related to the problem. Thanks.

Hi @pasin and @jens,

Thanks for the reply. For the information, when i called the addUser() method in the hotel-lister.js, it seem like the data is not added into the database. Because when I invoke the getUser() method, the result is empty.
Below is the code to my cordova plugin first.

hotel-lister.js

var hotelLister = {
  queryHotels: function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, 'HotelLister', 'queryHotels');
  },
  addUser: function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, 'HotelLister', 'addUser');
  },
  getUser: function(successCallback, errorCallback) {
    exec(successCallback, errorCallback, 'HotelLister', 'getUser');
  },
};

and here is my HotelLister extends CordovaPlugin java file.

public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
    if ("queryHotels".equals(action)) {
        ...			
    } else if ("addUser".equals(action)) {
		MutableDocument mutableDoc = new MutableDocument()
		.setString("name", "Amer")
		.setString("email", "amer@gmail.com")
		.setString("state", "Johor");

		try {
			this.userDB.save(mutableDoc);
			callbackContext.success();
			return true;
		} catch(CouchbaseLiteException e) {
			e.printStackTrace();
		}
		
    } else if ("getUser".equals(action)) {
		List<Result> resultList = queryUser();
        ArrayList<Map<String,Object>> arrayList = new ArrayList<>();
        assert resultList != null;
        for (Result result : resultList) {
            Map<String,Object> map = result.toMap();
            arrayList.add(map);
        }
        JSONArray jsonArray = new JSONArray(arrayList);
        callbackContext.success(jsonArray);
        return true;
		
	}
    return false;
}

and here is my queryUser method.

private List<Result> queryUser() {

    Query query = QueryBuilder
        .select(
            SelectResult.expression(Meta.id),
            SelectResult.property("name"),
            SelectResult.property("email"),
            SelectResult.property("state")
        )
        .from(DataSource.database(userDB));

    try {
        ResultSet resultSet = query.execute();
        return resultSet.allResults();
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
        return null;
    }
}

And here is how I initialize the database.

private DatabaseManager(Context context) {
    if (!Database.exists("travel-sample", context.getFilesDir())) {
        String assetFile = String.format("%s.cblite2.zip", DB_NAME);
        Utils.installPrebuiltDatabase(context, assetFile);
    }
    DatabaseConfiguration configuration = new DatabaseConfiguration(context);
    try {
        database = new Database(DB_NAME, configuration);
		userDB = new Database("user", configuration);
    } catch (CouchbaseLiteException e) {
        e.printStackTrace();
    }
}

You may want to step through code to confirm that the addUser method is intact being called.