CRUD with Phonegap/REST API

92 views
Skip to first unread message

dant

unread,
Apr 29, 2015, 3:45:28 PM4/29/15
to mobile-c...@googlegroups.com
I've been using happily Creating and Reading documents in my Phonegap application, following the TodoLite sample using the coax library. However, Updating using PUT doesn't seem to have any effect on my documents. I've tried a simple example on my device by calling this code added into TodoLite, which creates and gets a document, but does not update it:


var doc = {"name":"test doc", "checked":true};

config.db.post(doc, function (err, ok) {
        console.log("POST result: " + JSON.stringify(ok));

        config.db.get(ok.id, function (err, getdoc) {
            console.log("GET doc: " + JSON.stringify(getdoc));

            getdoc.name = "updated name";
            getdoc.checked = !getdoc.checked;
            config.db.put(getdoc._id, getdoc, function (err, putOk) {
               console.log("PUT updated doc response: " + JSON.stringify(putOk));
            });
       });
});

The output looks like this:

POST result: {"id":"93831e94-7128-4b26-ba47-930eebc9d33b","rev":"1-0bdb6591fa1c0e32565a3f59273ffa7f","ok":true}
GET doc: {"checked":true,"name":"test doc","_rev":"1-0bdb6591fa1c0e32565a3f59273ffa7f","_id":"93831e94-7128-4b26-ba47-930eebc9d33b"}
PUT updated doc response: {"checked":true,"name":"test doc","_rev":"1-0bdb6591fa1c0e32565a3f59273ffa7f","_id":"93831e94-7128-4b26-ba47-930eebc9d33b"}

So I'm not seeing any Logcat errors besides "authHeader is null" and "Unauthorized -- requestCredentials not given or do not match allowed credentials" but the document stays the same after attempting to PUT new values to it. I've been banging my head against this problem for a few days... am I doing something dumb here?

Couchbase Lite phonegap plugin v 1.0.4
Samsung Charge, Android v 2.3.6  (I will try on more modern phone as well)
Phonegap v4.2.0

Thank you,
Dan

Jens Alfke

unread,
Apr 29, 2015, 6:06:17 PM4/29/15
to mobile-c...@googlegroups.com
I don’t see anything wrong with your code, but I’m not very familiar with the JavaScript API. And our expert (JChris) is out on paternity leave. Is anyone else who’s currently online more of a JS expert?

One possibility: None of your code looks at the ‘err’ parameters passed to the callbacks. Maybe there’s an error there you haven’t noticed because you don’t log it?

—Jens

Dominique Legault

unread,
Apr 30, 2015, 11:28:34 AM4/30/15
to mobile-c...@googlegroups.com
Hi Dan,

Try specifying an id in the initial post.
And check for errors in the output.

'''js
if(err){console.log(JSON.stringify(err))}
else{console.log(JSON.stringify(result))}
'''

Cheers,
Dominique

Jens Alfke

unread,
Apr 30, 2015, 12:48:04 PM4/30/15
to mobile-c...@googlegroups.com

On Apr 30, 2015, at 8:28 AM, Dominique Legault <deefac...@gmail.com> wrote:

Try specifying an id in the initial post. 

POST doesn't require an _id. Its purpose is to create a random _id for the document (otherwise you'd just use PUT.)

—Jens

dant

unread,
May 1, 2015, 9:54:31 PM5/1/15
to mobile-c...@googlegroups.com
All the err's came back null.

I tried the same code on an Android 4.4.4 device and the PUT updated the doc successfully! 

This will work for me, I wasn't planning on supporting Gingerbread anyway. Thanks for the suggestions!

Dominique

unread,
May 2, 2015, 11:40:52 AM5/2/15
to mobile-c...@googlegroups.com

The coax library has really poor cross browser/ cross platform support. If you ever intend on using it for anything but the latest Android and IOS I would suggest using something else. I'm currently in the process of converting my project to use jquery ajax instead of coax.

--
You received this message because you are subscribed to a topic in the Google Groups "Couchbase Mobile" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mobile-couchbase/PlX7kDM0Q8w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mobile-couchba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mobile-couchbase/dafdda79-28e1-47ab-8982-b488bc3e7cce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages