1

I am trying to get a data from NeDB in a method in a .vue file using electron-vue. I know I can get that by putting that to a variable, but I want to get that by 'return' because I want to use the result in v-for.

I tried to use bluebird promisify and async/await but it's not work.

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')
})

main.js

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

test.vue

<template>
  <div>
    <ul>
      <li v-for="member in memberName">
        {{ member.name }}({{ member.relation }}){{ member._id }}
        <ul>
          <li v-for="game in filterByName(member._id)">
            {{ game }}
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
import Promise from 'bluebird'
export default {
  // some data
  created: function () {
    this.dbFindAsync = Promise.promisify(thistest.$db.find)
  },
  methods: {
    filterByName: async function (id) {
      const docs = await this.dbFindAsync({ 'members.nameId': id }, { 'members': 1, _id: 0 })
      console.log(docs)
      return docs
    },
  // some other methods
  }
}
</script>

I got "Uncaught (in promise) TypeError: Cannot read property 'push' of undefined".

I can get data from DB on created by this:

    this.$db.find({}, function (err, doc) {
      console.log(err)
      console.log(doc)
      this.list = doc || []
    }.bind(this))

Please help me....

user4565857
  • 155
  • 1
  • 2
  • 9

1 Answers1

0

Try to import on top of your script tag the nedb object.

something like this const Datastore = require("nedb") and then initialize a new doc

 db = new Datastore({
    filename: "a_collection_of_data",
    autoload: true,
    timestampData: true,
  });

Then in your methods you have access to the db object.

Let's say you want to retrieve a collection of data in the getData method. It would look something like this

getData() {
    db.find({}, function(err, doc) {
        console.log(err)
        console.log(doc)
        this.list = doc || []
    }.bind(this))
}
Admir Husić
  • 37
  • 10