I've been dealing with Couchbase-Lite-PhoneGap-Plugin.
I've try to create a design document in an Android emulator using REST API. But every time I get status: 400 - Bad request
message and I cannot find my mistakes.
My codes are the following;
var cblUrl = "";
var dbName = "user";
var app = {
initialize: function() {
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function() {
if(window.cblite) {
window.cblite.getURL(function(err, url) {
if(err) {
console.log("error launching Couchbase Lite: " + err);
} else {
console.log("Couchbase Lite running at " + url);
cblUrl = url;
}
});
} else {
console.log("error, Couchbase Lite plugin not found.");
}
// jQuery Main function
$(function() {
$("#connect").on("tap", function() {
$.ajax({
url: cblUrl + '_all_dbs',
type: 'GET',
success: function (data, textStatus, xhr) {
if (data.indexOf(dbName) == -1) { // db doesn't exist
// create a database (bucket)
$.ajax({
url: cblUrl + dbName,
type: 'PUT',
async: true,
success: function (data, textStatus, xhr) {
console.log(xhr.status);
},
error: function (xhr, statusText, errorThrown) {
console.log(xhr.status);
}
});
} else {
console.log("db exists");
}
},
error: function (xhr, statusText, error) {
console.log(xhr.status);
}
});
}); // #connect tap
// create button tap event
$("#create").on("tap", function() {
var view = {
"language" : "javascript",
"views" : {
"view_user" : {
"map" : "function(doc) {if (doc.username) {emit(doc.username, null)}}"
}
}
}
// ** create VIEW **
$.ajax({
url: cblUrl + dbName + "/_design/users",
type: 'PUT',
data: view,
success: function (data, textStatus, xhr) {
console.log(xhr.status);
},
error: function (xhr, statusText, error) {
console.log(xhr.status);
}
});
});
}); // main jQuery function
}, // onDeviceReady
};
app.initialize();
I had installed my plugin using command of
cordova plugin add com.couchbase.lite.phonegap
However the plugin was beta and a former version at plugins.cordova.io.
And then I've tried to uninstall and re-install plugin using command of
cordova plugin add https://github.com/couchbaselabs/Couchbase-Lite-PhoneGap-Plugin.git
I was able through this way.