2

I'm trying to use NeDB for my vue.js app, but the following error occurred when I tried to save my data from the second time.

The Error is:

"Can't insert key undefined, it violates the unique constraint."

I'm making the app in electron-vue.

I checked the database file and find that no _id field had been generated. I think the _id field should be generated automatically.

src\renderer\datastore.js

import Datastore from 'nedb'
import path from 'path'
import { remote } from 'electron'
export default new Datastore({
  autoload: true,
  filename: path.join(remote.app.getPath('userData'), '/data.db')
})

src\renderer\main.js

import db from './datastore'
Vue.prototype.$db = db

src\renderer\components\MyApp.vue

<template>
  <div>
    <form v-on:submit.prevent="insertShelf">

      <ul>
        <li>
          <label>Shelf Number</label>:
          <input type="text" name="shelfNum" v-model="newInput.shelfNum">
        </li>
      <ul v-for="(book, index) in newInput.books">
        <li>
          <label>Title</label>:
          <input type="text" name="title" v-model="book.title">
        </li>
        <li>
          <label>Author</label>:
          <input type="text" name="author" v-model="book.author">
        </li>
      </ul>
      <input type="submit" value="Submit">
    </form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      newInput: {
        shelfNum: '',
        books: [
          {
            title: '',
            author: ''
          }
        ]
      },
      shelfList: []
    }
  },
  watch: {
    shelfList: {
      handler: function () {
        this.$db.insert(JSON.stringify(this.shelfList), function (err, newDoc) {
          console.log('err', err)
          console.log('newDoc', newDoc)
        })
      }
    }
  },
  methods: {
    insertShelf: function () {
      this.shelfList.push({
        shelfNum: this.newInput.shelfNum,
        books: this.newInput.books
      })

      this.newInput = {
        shelfNum: '',
        books: [
          {
            title: '',
            author: ''
          }
        ]
      }
    }
  }
}
</script>

At the first time, the data I filled the form and send is saved successfully, but from the second time the error occurred.

The database file ( C:\Users\myaccount\AppData\Roaming\Electron\data.db ) has only this:

"[{\"shelfNum\":\"12345\",\"books\":[{\"title\":\"Winnie-the-Poo\",\"author\":\"A. A. Milne\"}]}]"

and _id field dose not be generated.

Please help me.

user4565857
  • 155
  • 1
  • 2
  • 9
  • 1
    https://github.com/louischatriot/nedb/issues/217#issuecomment-289945163 says "[I] realized I had set a unique index on a field, but the objects I was upserting did not contain that property. This caused the first record to be inserted, because the unique field had a value of undefined, but the next records would not insert because they had the same values for the unique field." That was my problem too. Maybe yours? – Lannie Apr 24 '19 at 19:23

0 Answers0