Cordova应用程序中的PouchDB同步

时间:2016-05-03 05:18:22

标签: cordova

我正在使用pouchdb使我的应用程序脱机。我创建了一个couchdb服务,在本地创建一个pouchdb数据库。我的代码正在使用浏览器做好所有事情。它在本地创建所有文档并与远程服务器很好地同步。但是当我在Android平台上的Cordova应用程序上运行代码时,它会创建本地数据库但不会与远程couchdb数据库同步数据......我的服务代码如下:

angular.module('couchDB.services', ["pouchdb"]).factory('CouchDBServices', CouchDBServices);

    CouchDBServices.$inject = ['$q'];

/**
 * @name CouchDBServices
 * @desc Singleton service class with static methods for actions with CouchDB.
 * @param $q - service that helps you run functions asynchronously, and use their return values
 *              when they are done processing
 * @param pouchDB - service with asynchronous APIs for work with couchDB
 * @param SERVER_ADDRESS - constant - remote url to couchdb instance
 */
function CouchDBServices($q) {


    var db = new PouchDB("user");
    console.log( db);
    var remote="https://couchdb-6ea670.smileupps.com/chat";

    PouchDB.sync(db, remote, {live: true, retry: true})

    return {
        getDocument:getDocument,
        getDocuments:getDocuments,
        createDocument:createDocument,
        updateDocument:updateDocument,
        checkUser:checkUser,
        auth:auth,
        conversationExists:conversationExists
    };

    //Document function

    /**
     * @name getDocument
     * @desc  function fetch specific document from couchdb database
     * @param id - document id
     * @returns document
     */
    function getDocument(id){




        console.log("CouchDBServices.getDocument()");
        var q = $q.defer();
        db.get(id).then(function (doc) {
            console.log("success: "+doc);
            q.resolve(doc);
        }).catch(function (err) {
            console.log("error; "+err);
            q.reject(err);
        });
        return q.promise;
    }

    /**
     * @name getDocuments
     * @desc  function fetch document's from couchdb database
     * @param ids - array of documents id
     * @returns documents
     */
    function getDocuments(ids){
        console.log("CouchDBServices.getDocuments()");
        var q = $q.defer();
        db.allDocs({
            keys:ids,
            include_docs:true
        }).then(function (doc) {
            console.log("success: "+doc);
            q.resolve(doc);
        }).catch(function (err) {
            console.log("error; "+err);
            q.reject(err);
        });
        return q.promise;
    }

    /**
     * @name createDocument
     * @desc  function create document in couchdb database
     * @param data - object with key - value properties
     * @returns information about successfully or not successfully created document
     */
    function createDocument(data){
        console.log("CouchDBServices.createDocument()");
        var q = $q.defer();


        db.put(data)
            .then(function (response) {
                console.log("success: "+JSON.stringify(response));
              q.resolve(response);
            }).catch(function (err) {
                q.reject(err);
              console.log("err: "+err);
            });
        return q.promise;
    }

    /**
     * @name updateDocument
     * @desc  function update specific document in couchdb database
     * @param id - document id
     * @param data - object with key - value properties
     * @returns information about successfully or not successfully created document
     */
    function updateDocument(id,data){
        console.log("CouchDBServices.updateDocument()");
        var q = $q.defer();
        db.get(id).then(function(doc) {
            return db.put(
                data,
                doc._id,
                doc._rev
            );
        }).then(function(response) {
            console.log("success: "+response);
            q.resolve(response);
        }).catch(function (err) {
            console.log("err"+err);
            q.reject(err);
        });
        return q.promise;
    }

    //Query methods - uses design documents

    /**
     * @name checkUser
     * @desc  function checks if user (user document) exists in database
     * @param userId - user id ie. document id
     * @returns object with document id and info that user exists,
     *          empty object if not exists and error if something isn't ok
     */
    function checkUser(userId){
        console.log("CouchDBServices.checkUser()");
        var q = $q.defer();
        db.query('user/userExists', {
            key          : userId,
            include_docs : false,
            limit        : 1
        }).then(function (res) {
            console.log("success: "+res);
            q.resolve(res);
        }).catch(function (err) {
            q.reject(err);
            console.log("err: "+err);
        });
        return q.promise;
    }

    /**
     * @name auth
     * @desc  function checks if user (user document) exists in database
     * @param user - object with user phone and password
     * @returns object with user document if exists,
     *          empty object if not exists and error if something isn't ok
     */
    function auth(user){
        console.log("CouchDBServices.auth()");
        var q = $q.defer();
        db.query('user/auth', {
            key         : [user.phone,user.password],
            include_docs : true
        }).then(function (res) {
            console.log("success: "+res);
            q.resolve(res);
        }).catch(function (err) {
            q.reject(err);
            console.log("err: "+err);
        });
        return q.promise;
    }

    /**
     * @name conversationExists
     * @desc  function checks if specific conversation document exists in database
     * @param chatId - id of conversation document
     * @returns object with conversation document if exists,
     *          empty object if not exists and error if something isn't ok
     */
    function conversationExists(chatId){
        console.log("CouchDBServices.conversationExists()");
        var q = $q.defer();
        db.query('conversation/chatExists', {
            key          : chatId,
            include_docs : true,
            limit        : 1
        }).then(function (res) {
            console.log("success: "+res);
            q.resolve(res);
        }).catch(function (err) {
            q.reject(err);
            console.log("err: "+err);
        });
        return q.promise;
    }
}

1 个答案:

答案 0 :(得分:0)

很可能你不在:

  1. 从设备到CouchDB的连接,
  2. 您的CouchDB未绑定到0.0.0.0,它绑定到127.0.0.1,这意味着它无法接受来自网络上其他设备的连接,
  3. 您的CouchDB没有配置CORS(尝试add-cors-to-couchdb)
  4. 它可能是这些中的任何一个。最简单的解决方法是使用ArrayList<RotatingEllipse> ellipses = new ArrayList<RotatingEllipse>(); void setup() { size(500, 500); ellipses.add(new RotatingEllipse(width*.25, height*.25)); ellipses.add(new RotatingEllipse(width*.75, height*.25)); ellipses.add(new RotatingEllipse(width*.75, height*.75)); ellipses.add(new RotatingEllipse(width*.25, height*.75)); } void draw() { background(0); for (RotatingEllipse e : ellipses) { e.stepAndDraw(); } } void mouseClicked() { ellipses.add(new RotatingEllipse(mouseX, mouseY)); } void mouseDragged() { ellipses.add(new RotatingEllipse(mouseX, mouseY)); } class RotatingEllipse { float rotateAroundX; float rotateAroundY; float distanceFromRotatingPoint; float angle; public RotatingEllipse(float startX, float startY) { rotateAroundX = (width/2 + startX)/2; rotateAroundY = (height/2 + startY)/2; distanceFromRotatingPoint = dist(startX, startY, rotateAroundX, rotateAroundY); angle = atan2(startY-height/2, startX-width/2); } public void stepAndDraw() { angle += PI/64; float x = rotateAroundX + cos(angle)*distanceFromRotatingPoint; float y = rotateAroundY + sin(angle)*distanceFromRotatingPoint; float distance = dist(x, y, width/2, height/2); float diameter = 50*(500-distance)/500; ellipse(x, y, diameter, diameter); } } 检查Android设备(在Chrome中打开;有说明here)。然后查看“网络”选项卡,查看错误是什么。