【问题标题】:Using design documents in pouchDB with crypto-pouch将 pouchDB 中的设计文档与 crypto-pouch 一起使用
【发布时间】:2016-05-07 15:21:56
【问题描述】:

在为我的 Ionic 项目测试 pouchDB 后,我尝试使用加密袋加密我的数据。但是我在使用设计文档时遇到了问题。我使用了以下代码:

  1. 我的一份设计文件:

    var allTypeOne = {
      _id: '_design/all_TypeOne',
        views: {
            'alle_TypeOne': {
            map: function (doc) {
                if (doc.type === 'type_one') {
                    emit(doc._id);
                }
            }.toString()
          }
        }
    };
    
  2. 初始化我的数据库:

    function initDB() {
      _db = new PouchDB('myDatabase', {adapter: 'websql'});
      if (!_db.adapter) {
        _db = new PouchDB('myDatabase');
      }
      return _db.crypto(password)
        .then(function(){
          return _db;
        });
    
      // add a design document
      _db.put(allTypeOne).then(function (info) {
    
      }).catch(function (err) {
    
      }
    }
    
  3. 获取type_one的所有文档:

    function getAllData {
      if (!_data) {
        return $q.when(_db.query('all_TypeOne', { include_docs: true}))
          .then(function(docs) {
            _data = docs.rows.map(function(row) {
              return row.doc;
            });
            _db.changes({ live: true, since: 'now', include_docs: true})
              .on('change', onDatabaseChange);
            return _data;
          });
      } else {
        return $q.when(_data);
      }
    }
    

此代码在不使用加密袋的情况下也能正常工作,但如果我插入 _db.crypto(...) ,我的列表中不会显示任何数据。谁能帮我?提前致谢!

【问题讨论】:

    标签: ionic-framework pouchdb documents


    【解决方案1】:

    我猜你的 put 是在对 crypto 的调用完成之前发生的。请记住,javascript 是异步的。因此,在放置您的设计文档之前,请等待加密调用完成。然后在全部完成后使用回调来访问您的数据库。类似于以下内容:

    function initDB(options) {
      _db = new PouchDB('myDatabase', {adapter: 'websql'});
      if (!_db.adapter) {
        _db = new PouchDB('myDatabase');
      }
    
      _db.crypto(password)
        .then(function(){
           // add a design document
           _db.put(allTypeOne).then(function (info) {
             options.success(_db);
           })
          .catch(function (err) { console.error(err); options.error(err)})
        .catch(function (err) { console.error(err); options.error(err);})
      }
    }
    
    initDB({
      success:function(db){
        db.query....
      }
    )
    

    【讨论】:

    • 我以前试过这个,但没有帮助。同时我发现问题出在监听器的变化上。当我删除它时,加密甚至设计文档都可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-31
    相关资源
    最近更新 更多