Error inserting data into Lokijs with Vuejs

Asked

Viewed 170 times

0

Hello, I am starting in Electron with Vuejs1.0 && Lokijs and I have had difficulty finding the solution to the error that happens whenever I will insert a second given to Collection:

Error:

Uncaught Error: Document is already in Collection, Please use update()

customer registration.html

<form class="pane-body" id="formCadastrarCliente">
        <h3>Cadastro de Cliente</h3>
        <div class="form-group form-nome">
          <input type="text" id="nome" class="form-control" v-model="cliente.nome" placeholder="Nome" />
        </div>
        <div class="form-group form-dataNasc">
          <input type="date" id="dataNasc" class="form-control" v-model="cliente.dataNasc" placeholder="DataNasc">
        </div>
        <div class="form-group form-cpf">
          <input type="text" id="cpf" class="form-control" v-model="cliente.cpf" placeholder="CPF">
        </div>

        <a href="index.html"><button type="button" id="cancelar" class="btn btn-danger">Cancelar</button></a>
        <button type="button" @click="salvarCliente()" class="btn btn-primary">Salvar</button>
</form>

customer register.js

var read = require('read-file-utf8')
var loki = require('lokijs');// instanciando o Banco de dados
var db = new loki('db.json');//Criando o banco de dados 
var data = read(__dirname + '/db.json');
db.loadJSON(data);

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

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


window.Vue = require('vue');
new Vue({
    el: 'body',
    data: {
        cliente: {/*objeto Cliente com valores default*/
            nome: '',
            dataNasc: '',
            cpf: ''
         }

    },
    methods:
    {
        salvarCliente: function ()
        {
            if (this.cliente.nome != '')
            {
                clientes.insert(this.cliente);
                db.save();
                document.getElementById("formCadastrarCliente").reset();
            }
        }
    }
})

I insert the data into the form, save them, clear the form and when I insert new data and have saved accuses the error:

Uncaught Error: Document is already in Collection, Please use update()

If anyone knows my mistake, I’d appreciate it!

  • VueJS1.0? Are you sure you want to use version 1?

  • As I am learning now, I decided to follow a tutorial in which they used version 1.0. But if version 2.0 solves this problem, I can migrate to it kkk

  • Definitely use Vue 2. Version 3 is about to come out. Test with Vue 2 and if you can’t tell me.

  • I haven’t switched to Vuejs2.0 yet, but I found the error. When I inserted the first object in Collection Client Loki JS added another attribute to the object with default values: the $Loki, which is the primary key. And because of the data-Binding $Loki was kept for the following inserts, repeating the primary key. I re-used the save methodClient: <br/>

  • salvarCliente: function ()&#xA; { let cli : { nome : this.cliente.nome, cpf: this.cliente.cpf, dataNasc : this.cliente.dataNasc}&#xA; if (cli.nome != ' ')&#xA; { clients.Insert(cli); db.save(); Document.getElementById("formCadastrarClient"). reset(); } }

  • So don’t take the $Loki. Thank you!!!

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.