【问题标题】:pouchdb seems to be saving but does not savepouchdb 似乎在保存但不保存
【发布时间】:2019-01-09 23:09:59
【问题描述】:

我正在开发一个 cordova 应用程序。由于本地存储空间不足,我开始使用 pouchdb。我将保存在本地存储区的json数据发布到pouchdb。就像创建了一个文档。但该文件不会出现。当我使用 Fetch All Documents 函数时返回 0 行。

create: function (item) {
            myApp.db.post(item).then(function (response) {
                console.log("Response id " + response.id + " item " + item);
            }).catch(function (err) {
                console.log(err.name === 'conflict' ? "Conflict occurred - possible duplicate " : "Error " + err);
            });
  },
    loadData: function (callback) {
        myApp.db.allDocs({ include_docs: true, attachments: true }, function (err, response) {
            if (err) console.log(err);
            var rows = response.rows;
            for (var i = 0; i < rows.length; i++) {
                console.log(rows[i].doc);
                alert(JSON.stringify(rows[i].doc));
                //var taskItem = myApp.services.tasks.createTaskElem(rows[i].doc);
                //if (rows[i].doc.completed)
                //    myApp.services.tasks.addToCompletedList(taskItem);
                //else myApp.services.tasks.addToPendingList(taskItem);
            }
        });
    }
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;

【问题讨论】:

    标签: cordova pouchdb


    【解决方案1】:

    我相信您被 Javascript 的异步特性所欺骗。在处理您的 post 命令时,Javascript 会继续执行下一个任务 ...allDocs ... 并没有找到您的数据,因为它仍在写入 PouchDB。

    如果您已经注意到,每个 PouchDB API 文档示例都有 3 个选项卡,可让您选择 Callback、Promise 或 Async/Await 替代项。我相信你会发现,一旦你掌握了它的诀窍,Async/Await 到目前为止是使用 PouchDB 的最佳方式。

    所以,请尝试以下方法,并告诉我你的进展情况:

    /*  Function to instantiate a new random JSON record */
    const item = () => {
      return {
        _id: `Thing_${parseInt(1000*Math.random(), 10)}`,
        stuff: ' silly random remark ',
      }
    };
    
    /* Function to 'put', then retrieve, records from PouchDB */
    const dbTest = async () => { // "async" advises JavaScript to expect (otherwise illegal) 'await' statements
      console.log(`
        Start ...
        `);
      try {
        /* Remember the item ID */
        const rndItemId = item();
    
        /* Store the random item in PouchDB */
        const response = await myapp.db.put(rndItemId); // Do NOT return until 'put' is complete
    
        /* Log the response from PouchDB */
        console.log(`response for <${rndItemId._id}> ....\n${JSON.stringify(response, null, 2)}`);
    
        /* Get back all the random items we 'put' in the Pouch so far */
        const result = await myapp.db.allDocs({ // Do NOT return until 'allDocs' is complete
          include_docs: true,
          attachments: true,
          startkey: 'Thing_0',
          endkey: 'Thing_999',
        });
    
        /* Log the result from PouchDB */
        console.log(`result ....\n${JSON.stringify(result, null, 2)}`);
    
      } catch (err) {
        console.log(err);
      }
      console.log(`
        ... done!
        `);
    };
    /* Call the above function */
    dbTest();
    /* Call it again */
    dbTest();
    

    快速提示:注意使用put 而不是post!请阅读12 pro tips for better code with PouchDB

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2017-09-07
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      相关资源
      最近更新 更多