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.