【问题标题】:How to store offline form data in pouchDB?如何在 pouchDB 中存储离线表单数据?
【发布时间】:2015-05-23 17:22:46
【问题描述】:

我在尝试在 pouch db 中插入数据时遇到错误。 我通过在 youtube 上看到你的视频来执行基本的 crud 操作。 但是在检索表时出现此错误

未捕获的类型错误:db.alldocs 不是函数

我是单页应用程序的新手。

这是我的代码

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>APP</title>

        <script src="//cdn.jsdelivr.net/pouchdb/3.5.0/pouchdb.min.js"></script>

        <script>
        var db = new PouchDB('courts');
        function searchCase(){
            var caseno = window.document.courtform.caseno.value;
            var casetype = window.document.courtform.casetype.value;
            var year = window.document.courtform.year.value;

            var store={
                    _id: caseno,
                    type: casetype,
                    yr: year


            };
            db.put(store, function callback(error,result)
            {
                if(!error){
                    clearfields();
                    document.getElementById("message").innerHTML=
                        "succesfully added";
                }
                alert("success");
            });
        }
        function showCase(){
            db.alldocs({include_docs: true, descending: true},
                    function(err,doc){

                showTableofCases(doc.rows);
                } );
             function showTableofCases(data){
                var div = document.getElementById("message");
                var str="<table border='1' align= 'center'><tr><th>xxx</th>"+

                "<th>vvv</th><th>hhh</th></tr>";
                for(var i=0;i<data.length;i++){
                    str+= "<tr><td>"+data[i].doc._id+
                    "</td><td>"+data[i].doc.type+
                    "</td><td>"+data[i].doc.year+"</td></tr>";
                }
                str+="</table>";
                div.innerHTML = str;

             }

        }
        function clearfields(){
            window.document.courtform.caseno.value="";
            window.document.courtform.casetype.value="";
            window.document.courtform.year.value="";
        }
 </script>

    </head>
    <body>
       <form name="courtform">

     <div align="center">
                     Case No<br/><input type="text" name="caseno" /><br/><br/>
                       Case Type
                        <select id="casetype" name="casetype" >
                            <option value="1" selected>WA</option><option value="2">WP</option><option value="3">SA</option><option value="4">AS</option></select><br/><br/>

                        Case Year<select id="year" name="year" >
                            <option value="0">2015</option>
                            <option value="1">2016</option>

                        </select><br/><br/>


                <input type="button"  value="Save" onclick="searchCase()" />

                <input type="button" value="show" onclick="showCase()" />

                            <input type="button" value="Clear" onclick="clearfields()"/> 

           </div> </form>
           <div id="message"></div>
    </body>
</html>

告诉我 igoi 哪里错了??? Morover 我很高兴创造这项新技术的人回答了我的问题....那是 gr8!!!! 提前谢谢!!!!

【问题讨论】:

  • 如果您正确地标记和标题,您将获得更多帮助。 PouchDB 不是 HTML5 本地存储,与 HTML 无关。它是一个开源的 JavaScript 数据库,StackOverflow 有它自己的标签。所以把PouchDB放在你的标题里,把tag改成PouchDB,你可能会得到一些答案。

标签: pouchdb


【解决方案1】:

错误在于 1) 您是 put()ing 一个没有 _id 的文档,并且 2) 该文档有一个以 _ (_case) 开头的变量。这在 PouchDB 中是不允许的;以_ 开头的唯一有效标识符是_id_rev_attachments

此外,如果您在对put() 的回调中将错误console.loged,那么您会看到错误:

{ status: 412, 
  name: "missing_id", 
  message: "_id is required for puts", 
  error: true }
{ status: 500, 
  name: "doc_validation", 
  message: "Bad special document member: _case", 
  error: true, 
  reason: "_case" }

不幸的是,这是编写异步代码时的常见错误!您应该始终处理错误并将其打印出来。 :)

【讨论】:

  • 请回复我的查询,因为我已按照您的 tutorial 在这里PouchDB jump start 更改了它
  • 您的新错误是您要输入db.allDocs,而不是db.alldocs。顺便说一句,我们通常在#pouchdb IRC 频道上反应迅速,因此如果您需要更多帮助,您可能想尝试一下。 :)(点击pouchdb.com底部的“IRC”链接)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2016-12-08
  • 1970-01-01
相关资源
最近更新 更多