Couchbase lite / com.couchbase.lite.phonegap: startKey and endKey a rest api GET

81 views
Skip to first unread message

Pelle Krøgholt

unread,
Jun 5, 2014, 6:06:05 AM6/5/14
to mobile-c...@googlegroups.com
Hi,

I have the todo phonegab app running (https://github.com/couchbaselabs/TodoLite-PhoneGap using the com.couchbase.lite.phonegap plugin) on an android 4.4 with chrome developer tools setup to interact with the android device.

through the device i have added two lists and 7 tasks (6 for one list and 1 for another list ) and they all show up in the ui in the right list - nice :)

now I like to do some basic simple operations on the couchbase lite using $ajax for simplicity (we have already zepto loaded). 

I understand that the example uses the coax / hoax libraries - but for a very simply use case i like to speak directly to the couchbase lite database with basic $ajax.

I can successfully make a GET:

> $.ajax({
    type: "GET",
    contentType: "application/json",
    success: function (msg) { console.log('succes: ', msg) },
    error: function (err){ console.log('error: ', err)} 
});
succes: Object {..., title: "task 1", ...}

now I like to get the tasks view in descending order - works(TM):

> $.ajax({
    type: "GET",
    contentType: "application/json",
    success: function (msg) { console.log('succes: ', msg) },
    error: function (err){ console.log('error: ', err)} 
});
succes:  
Object {offset: 0, total_rows: 7, rows: Array[7]}

if I set it to false it shows in ascending order.

fine we got all tasks from all lists - now I like to restrict the tasks to be only those with list_id "969aee54-76e0-46c7-a585-fa5c13d619d6" - in the todo app (index.js) this is done:

window.dbChanged = function() {
    config.views(["tasks", {
startkey : [id, {}],
endkey : [id],
descending : true
    }], function(err, view) {
...

how to do the same with the above $ajax/uri approach? i have tried:

> var startKey = encodeURIComponent('["969aee54-76e0-46c7-a585-fa5c13d619d6", {}]')
> var endKey = encodeURIComponent('["969aee54-76e0-46c7-a585-fa5c13d619d6"]')
> $.ajax({
    type: "GET",
    contentType: "application/json",
    success: function (msg) { console.log('succes: ', msg) },
    error: function (err){ console.log('error: ', err)} 
    });
succes:  
Object {offset: 0, total_rows: 7, rows: Array[7]}

hmm but that gives me 7 tasks i had only expected 6 - any hints/suggestions ?

thanks

//

pelle

Matt Quinn

unread,
Jun 5, 2014, 1:16:20 PM6/5/14
to mobile-c...@googlegroups.com
On Thu, Jun 05, 2014 at 03:06:05AM -0700, Pelle Krøgholt wrote:
> how to do the same with the above $ajax/uri approach? i have tried:
>
> > var startKey =
> encodeURIComponent('["969aee54-76e0-46c7-a585-fa5c13d619d6", {}]')
> > var endKey =
> encodeURIComponent('["969aee54-76e0-46c7-a585-fa5c13d619d6"]')
> > $.ajax({
> type: "GET",
> url:
> "http://localhost:5984/todo/_design/todo9/_view/tasks?startKey="+startKey+"&endKey="+endKey,
> contentType: "application/json",
> success: function (msg) { console.log('succes: ', msg) },
> error: function (err){ console.log('error: ', err)}
> });
> succes:
> Object {offset: 0, total_rows: 7, rows: Array[7]}
>
> hmm but that gives me 7 tasks i had only expected 6 - any hints/suggestions
> ?

I think this is because of your query string parameters names. Can you
try it again using "startkey" and "endkey" (all lower case)?

Matt

Pelle Krøgholt

unread,
Jun 6, 2014, 5:12:13 AM6/6/14
to mobile-c...@googlegroups.com, ma...@mjquinn.ca
thanks for the suggestion - when I try with lowercase I get unfortunately an empty result:

    contentType: "application/json",
    success: function (msg) { console.log('succes: ', msg) },
    error: function (err){ console.log('error: ', err)} 
});
succes:  
Object {offset: 0, total_rows: 0, rows: Array[0]}


//

pelle


 

Matt

Pelle Krøgholt

unread,
Jun 6, 2014, 6:36:41 AM6/6/14
to mobile-c...@googlegroups.com, ma...@mjquinn.ca

ok once again thanks matt for pointing me in the right direction - i must have been a bit dizzy when I look at it again with fresh eyes.

solution: lowercase startkey and endkey + descending=true

and now i get the 6 items as expected:

$.ajax({
    type: "GET",
    url: 'http://localhost:5984/todo/_design/todo9/_view/tasks?startkey=["969aee54-76e0-46c7-a585-fa5c13d619d6", {}]&endkey=["969aee54-76e0-46c7-a585-fa5c13d619d6"]&descending=true',
    contentType: "application/json",
    success: function (msg) { console.log('succes: ', msg)},
    error: function (err){ console.log('error: ', err)} 
});
succes:  
Object {offset: 0, total_rows: 6, rows: Array[6]}

Traun Leyden

unread,
Jun 6, 2014, 11:45:59 AM6/6/14
to mobile-c...@googlegroups.com
Glad to hear you got it working!

Thanks Matt for helping out -- much appreciated. 


--
You received this message because you are subscribed to the Google Groups "Couchbase Mobile" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/ca1cfdb1-98ea-4055-85af-1ae8c0398810%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages