使用VueJS将数据插入LokiJS时出错

时间:2019-05-15 00:16:08

标签: javascript vue.js electron lokijs

我从VueJS1.0 && LokiJS开始使用Electron,但是我很难找到每次我向Collection中插入第二个数据时发生的错误的解决方案:

错误:

  

未捕获的错误:文档已经在收集中,请使用update()

insert-client.html

    <form class="pane-body" id="formInsertClient">
            <h3>Insert Client</h3>
            <div class="form-group form-nome">
              <input type="text" id="name" class="form-control" v-model="client.name" placeholder="Name" />
            </div>
            <div class="form-group form-dateNasc">
              <input type="date" id="dateNasc" class="form-control" v-model="client.dateNasc" placeholder="DateNasc">
            </div>
            <div class="form-group form-cpf">
              <input type="text" id="cpf" class="form-control" v-model="client.cpf" placeholder="CPF">
            </div>

            <a href="index.html"><button type="button" id="cancel" class="btn btn-danger">Cancel</button></a>
            <button type="button" @click="insertClient()" class="btn btn-primary">Save</button>
    </form>

insert-client.js

    var read = require('read-file-utf8')
    var loki = require('lokijs');
    var db = new loki('db.json'); 
    var data = read(__dirname + '/db.json');
    db.loadJSON(data);

    var clients = db.getCollection('clients');
    if (!clients)
    {
        clients = db.addCollection('clients');
        db.save();
    }

    function ready(fn) {
        if (document.readyState != 'loading')
        {
          fn();
        } else {
            document.addEventListener('DOMContentLoaded', fn);
        }
    }


    window.Vue = require('vue');
    new Vue({
        el: 'body',
        data: {
            client: {
                name: '',
                dateNasc: '',
                cpf: ''
             }

        },
        methods:
        {
            insertClient: function ()
            {
                if (this.client.name != '')
                {
                    clients.insert(this.client);
                    db.save();
                    document.getElementById("formInsertClient").reset();
                }
            }
        }
    })

我在表单中输入数据,将其保存,清除表单,然后在我输入新数据并发送并保存时确认错误:

  

未捕获的错误:文档已经在收集中,请使用update()

如果有人知道我的错误,请提前谢谢!

0 个答案:

没有答案