如何在pouchDB中存储离线表单数据?

时间:2015-05-22 13:25:22

标签: pouchdb

我在尝试在数据包db中插入数据时遇到错误。 通过在youtube中观看您的视频,我可以执行基本的操作。 但是在检索表格时出现此错误

未捕获的TypeError: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 !!!! Thx提前!!!!

1 个答案:

答案 0 :(得分:3)

错误是1)您put()ing没有_id的文档,2)文档的变量以__case)开头。 PouchDB中不允许这样做;以_开头的唯一有效标识符为_id_rev_attachments

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

{ 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" }

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

相关问题