作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想通过使用 electron + rxdb 学习和开发桌面应用。
我的文件结构:
main.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 = "";
}
每当我运行这个应用程序时,我都会遇到这些错误:
我是编程新手,尤其是js,我已经被这些错误卡住了一个星期,就是无法让它工作。有什么帮助吗?谢谢。
最佳答案
问题是这一行在 main.js 中:
const dbjs = require('./js-server/db.js')
为什么?因为您需要在主进程中使用 RxDB
并使用 IndexedDB 适配器。 IndexedDB 是一种浏览器 API,因此只能在渲染过程中使用。在 Electron 中,主进程是一个纯 Node/Electron 环境,无法访问 Chromium API。
如果您想将数据库保存在单独的线程中,请考虑生成一个新的隐藏浏览器窗口:
import {BrowserWindow} from 'electron'
const dbWindow = new BrowserWindow({..., show: false})
然后使用 IPC 在两个窗口之间进行通信,类似于您已经完成的操作。
使用只需要 NodeJS API 的 levelDB 适配器,这样您就可以将数据库保留在主进程中。
关于javascript - 如何让 Electron + rxdb 工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49356898/
我是一名优秀的程序员,十分优秀!