2016-09-16 53 views
0

我是新手到couchbase精简版,我需要你的帮助。 我想要离线存储数据和访问并显示在phonegap应用程序中。所以我选择了couchbase lite。从phonech中检索从couchbase lite整个文档

使用的示例代码做一些step.There是

  • 添加couchbase精简版框架。
  • 在phonegap应用程序中运行它。
  • 获取本地couchbase lite URL。
  • 创建数据库。
  • 用于插入,更新和删除文档的数据库连接。

    但我的问题从数据库检索整个文档。 同时我读取地图方法来检索文档。但我无法理解。

    样品PhoneGap的应用:Here the link

    下面我附上了工作代码,测试它在iOS平台

    var coax = require("coax"); 
    console.log(coax); 
    var appDbName = "couchdb"; 
    document.addEventListener('deviceready', onDeviceReady, false); 
    
    // deviceready Event Handler 
    // 
    // The scope of 'this' is the event. In order to call the 'receivedEvent' 
    // function, we must explicitly call 'app.receivedEvent(...);' 
    
    function onDeviceReady() { 
    receivedEvent('deviceready'); 
    setupConfig(function(err){ 
          if (err) { 
          alert(err) 
          return console.log("err "+JSON.stringify(err)) 
          } 
          }); 
    
    } 
    
    function logMessage(message) { 
    var p = document.createElement("p"); 
    p.innerHTML = message; 
    document.body.getElementsByClassName('app')[0].appendChild(p); 
    console.log(message); 
    } 
    
    // Update DOM on a Received Event 
    
    function receivedEvent(id) { 
    var parentElement = document.getElementById(id); 
    var listeningElement = parentElement.querySelector('.listening'); 
    var receivedElement = parentElement.querySelector('.received'); 
    
    listeningElement.setAttribute('style', 'display:none;'); 
    receivedElement.setAttribute('style', 'display:block;'); 
    
    console.log('Received Event: ' + id); 
    } 
    
    
    
    function setupConfig(done) { 
    // get CBL url 
    if (!window.cblite) { 
        return done('Couchbase Lite not installed') 
    } 
    cblite.getURL(function(err, url) { 
           console.log("getURL: " + JSON.stringify([err, url])); 
    
           window.server = coax(url); 
    
           var db = coax([url, appDbName]); 
    
           setupDb(db, function(err, info){ 
             console.log("getDB"+db); 
    
             if (err) { 
             return alert(JSON.stringify("GetDB:"+ err)); 
             } 
    
             db.get("_local/user", function(err, doc) { 
             if (err) { 
    
              if(err.status == 404) { 
    
    
              var docV = { "key" : "value" }; 
              db.put("_local/user", docV, function(err, ok) { 
              //HERE I AM INSERT THE DATA 
              return alert(JSON.stringify("Success:" + ok)); 
              }); 
    
    
              } 
              else { 
              return alert(JSON.stringify(err)); 
              } 
    
             } 
             else { 
              console.log("Document : "+doc._id); 
              // HERE I AM UPDATE AND DELETE THE DATA 
              doc._deleted =true; 
              db.put("_local/user", doc, function(error, ok) { 
    
              }); 
    
    
              } 
    
             }); 
    
    
             }); 
    
           }); 
    
    } 
    
        function setupDb(db, cb) { 
         db.get(function(err, res, body){ 
         console.log(JSON.stringify(["before create db put", err, res, body])) 
         db.put(function(err, res, body){ 
           db.get(cb); 
           }) 
         }) 
    } 
    

回答

0

你得到传过来的JS变种的文档。下面是显示了这个(从你链接的示例应用程序),一个小片断:在文档

function toggleChecked(id) { 
    log("toggle", id) 
    config.db.get(id, function(err, doc){ 
     doc.checked = !doc.checked 
     doc.updated_at = new Date() 
     config.db.put(id, doc, function(){}) 
    }) 
} 

属性都可以直接更新。