2016-09-16 5 views
0

私はcouchbaseライトに初心者です。私はあなたの助けが必要です。 データを保存して、オフラインでアクセスし、phonegapアプリに表示したいとします。だから私はcouchbaseライトを選択しています。電話帳でcouchbase liteからドキュメント全体を取得

一部step.Thereを行うために使用するサンプルコードは

  • のCouchbase liteのフレームワークを追加します。
  • phonegapアプリで実行します。
  • ローカルcouchbaseライト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のVARとして渡されたドキュメントを取得します。これを示す小さなスニペットが表示されます(リンクしたサンプルアプリから):

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(){}) 
    }) 
} 

docのプロパティが利用可能であり、直接更新されています。

関連する問題