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();
}
}