我目前正在使用Electron和MongoDB构建桌面应用程序。该应用程序的目标是在应用程序本地作用域(而不是服务器上)中收集和存储各个客户的信息。我已经研究了使用node.js的MongoDB,但是我还没有找到在Electron中使用它的方法。
我目前正在使用Electron和MongoDB构建桌面应用程序。该应用程序的目标是在应用程序本地作用域(而不是服务器上)中收集和存储各个客户的信息。我已经研究了使用node.js的MongoDB,但是我还没有找到在Electron中使用它的方法。
这是一个用于 MongoDB 管理的Electron 应用程序,您可以查看其代码示例以了解如何在 electron 中使用 mongodb。
https://github.com/officert/mongotron
基本上,您可以在主进程中像在 Node.js 中一样正常使用 mongodb,然后通过 ipc 模块 与渲染进程通信。
例如:
渲染进程
<html>
<head></head>
<body>
<script>
const ipc = require('electron').ipcRenderer;
const informationBtn = document.getElementById('information-dialog')
informationBtn.addEventListener('click', function (event) {
ipc.send('create-user')
})
</script>
</body>
<html>
主进程
const ipc = require('electron').ipcMain
const mongo = require('some-mongo-module')
ipc.on('create-user', function (event) {
/* MONGODB CODE */
})
我建议你使用在http://electron.atom.io/找到的入门应用程序。
您可以使用 MongoDB Realm,它具有与 Mongo 驱动程序类似的 MQL API。
https://docs.mongodb.com/realm/sdk/node/integrations/electron-cra/
我正在使用Electron + React + Typescript,我需要像这样导入mongoose:
const mongoose = window.require("mongoose");
// then you can connect to it like so:
mongoose.connect("mongodb://localhost:27017/test");
这将解决错误:
TypeError: mongoose.connect is not a function