【问题标题】:How to get Electron + rxdb to work?如何让 Electron + rxdb 工作?
【发布时间】:2018-08-27 15:43:08
【问题描述】:

我想通过使用 electron + rxdb 来学习和开发一个桌面应用程序。

我的文件结构:

  • main.js(electron的主进程)
  • /js-server/db.js(所有关于 rxdb 数据库,包括创建)
  • /js-client/ui.js(electron的渲染器进程)
  • index.html(html 主页)

ma​​in.js 代码

const electron = require('electron')
const dbjs = require('./js-server/db.js')
const {ipcMain} = require('electron')
ipcMain.on('search-person', (event, userInput) => {
  event.returnValue = dbjs.searchPerson(userInput);
})

db.js 代码:

var rxdb = require('rxdb');
var rxjs = require('rxjs');
rxdb.plugin(require('pouchdb-adapter-idb'));
const personSchema = {
    title: 'person schema',
    description: 'describes a single person',
    version: 0,
    type: 'object',
    properties: {
        Name: {type: 'string',primary: true},
        Age: {type: 'string'},
    },
    required: ['Age']
};
var pdb;
rxdb.create({
    name: 'persondb',
    password: '123456789',
    adapter: 'idb',
    multiInstance: false
}).then(function(db) {
    pdb = db;
    return pdb.collection({name: 'persons', schema: personSchema})
});

function searchPerson(userInput) {
    pdb.persons.findOne().where('Name').eq(userInput)
    .exec().then(function(doc){return doc.Age});
}
module.exports = {
    searchPerson: searchPerson
}

ui.js 代码:

const {ipcRenderer} = require('electron');
function getFormValue() {
    let userInput = document.getElementById('searchbox').value;
    displayResults(ipcRenderer.sendSync("search-person",userInput));
    document.getElementById('searchbox').value = "";
}

每当我运行这个应用程序时,我都会收到以下错误:

  1. (节点:6084)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝 id:2):错误:RxError: RxDatabase.create():未添加适配器。 (我确定我已经成功安装了 pouched-adapter-idb 模块)
  2. 类型错误,无法读取未定义的属性“persons”。 (当我在 index.html 中搜索并按回车键时会弹出此错误)

我是编程新手,尤其是 js,我被这些错误困扰了一个星期,就是无法让它工作。有什么帮助吗?谢谢。

【问题讨论】:

    标签: javascript node.js electron


    【解决方案1】:

    问题是这一行在main.js中:

    const dbjs = require('./js-server/db.js')
    

    为什么?因为您需要在主进程中使用 RxDB 并使用 IndexedDB 适配器。 IndexedDB 是一个浏览器 API,因此只能在渲染过程中使用。在 Electron 中,主进程是纯 Node/Electron 环境,无法访问 Chromium API。

    选项#1

    如果您想将数据库保存在单独的线程中,请考虑生成一个新的隐藏浏览器窗口:

    import {BrowserWindow} from 'electron'
    const dbWindow = new BrowserWindow({..., show: false})
    

    然后使用 IPC 在两个窗口之间进行通信,就像您已经完成的那样。

    选项 #2

    使用只需要 NodeJS API 的 levelDB 适配器,这样您就可以将数据库保留在主进程中。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2011-04-07
      • 2011-07-27
      相关资源
      最近更新 更多