1

我正在尝试使用 electron-vue 以 .vue 文件中的方法从 NeDB 获取数据。我知道我可以通过将它放入一个变量来得到它,但我想通过'return'得到它,因为我想在 v-for 中使用结果。

我尝试使用 bluebird promisify 和 async/await 但它不起作用。

数据存储.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

测试.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>

我得到“未捕获(承诺)类型错误:无法读取未定义的属性'推送'”。

我可以从由此创建的数据库中获取数据:

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

请帮我....

4

1 回答 1

0

尝试在您的脚本标记顶部导入nedb对象。

像这样const Datastore = require("nedb") ,然后初始化一个新文档

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

然后在您的方法中,您可以访问该db对象。

假设您想在 getData 方法中检索数据集合。它看起来像这样

getData() {
    db.find({}, function(err, doc) {
        console.log(err)
        console.log(doc)
        this.list = doc || []
    }.bind(this))
}
于 2020-12-11T15:40:58.800 回答