Remote access fails: an example of accessing cloudant.com from Android, please!

78 views
Skip to first unread message

Charles Palmer

unread,
Mar 16, 2017, 8:30:53 PM3/16/17
to PouchDB
I am trying out PouchDB on an Android phone. Local database access works fine but my attempts to access a remote database at cloudant.com fail with behaviour that I cannot understand.Could someone please publish a "real world" example of an Android app accessing a remote cloudant.com database, including all aspects of authentication and CORS issues.

Here is the code and the console output:

    remoteDb = new PouchDB("https://my_account_here.cloudant.com/mmm_test_1/",
        {auth: {username: "my_username_here",
        password: "my_password_here"},
        ajax: {headers: {"Access-Control-Allow-Origin": "*"}}});

    console.log("remoteDb created: " + JSON.stringify(remoteDb));
    console.log("remoteDb name: " + remoteDb.name);

    localDb.replicate.to(remoteDb, {
        live: false,
        retry: false
    }).on("change", function(info) {
        console.log("On change");
        console.log(info);
    }).on("error", function(error) {
        console.log("On error " + JSON.stringify(error) );
    }).catch(function(error) {
        console.log("remoteDb is now " + JSON.stringify(remoteDb));
    });

Console output is:
remoteDb created: {"__opts":{"auth":{"username":"<username here","password":"<password here>"},"ajax":{"headers":{"Access-Control-Allow-Origin":"*"}},"name":"https://my_account_here.cloudant.com/mmm_test_1/","adapter":"https"},"prefix":"_pouch_","name":"https://my_account_here.cloudant.com/mmm_test_1/","_adapter":"https","_events":{},"taskqueue":{"isReady":false,"failed":false,"queue":[]},"adapter":"https"}

remoteDb name: https://acutetech.cloudant.com/mmm_test_1/

On error {"status":0,"name":"unknown","message":"getCheckpoint rejected with ","result":{"ok":false,"start_time":"2017-03-14T20:57:35.659Z","docs_read":0,"docs_written":0,"doc_write_failures":0,"errors":[],"status":"aborting","end_time":"2017-03-14T20:57:35.716Z","last_seq":0}}

[Note the "remoteDb is now" console line is not printed, and the "name":"unknown" seems odd to me.]

Immediately after this I execute this line: console.log("remoteDb is now " + JSON.stringify(remoteDb));

and I get "[ERR] Uncaught TypeError: Converting circular structure to JSON"

I am using pouchdb-6.1.2.js - when I use pouchdb-3.3.0.min.js I get a different set of errors.

I have confirmed that I am able to access my cloudant.com database with the same username and password (see my Node-RED experiments here: http://flows.nodered.org/flow/411bab7d9872bac32ad1ebba06c14a37).

Google - and PouchDB documents - suggest CORS might be the issue, but don't provide details of fixing this.

Help, please. A fully worked example, rather than snippets, would be most valuable
Reply all
Reply to author
Forward
0 new messages