我正在用NeDB开发Vue电子。在使用NeDB时,我遇到了问题,NeDB虽然设置了filename和autoload: true选项,但没有保存本地文件。
当加载NeDB时,我尝试了db对象的输出日志,它正确设置了路径。
Datastore {inMemoryOnly: false,
autoload: true,
timestampData: false,
filename: "./db/nedb.db",
compareStrings: undefined, …}
autoload: (...)compareStrings: (...)
executor: Executorfilename: "./db/nedb.db"
inMemoryOnly: falseindexes: Objectpersistence: PersistencetimestampData: ...我看到了其他的帖子。但我不知道该怎么做Then in the renderer process get the datastore via Electron.Remote 电子app中的NEDB持久性
我认为可能会发生这种情况,因为NeDB需要在运行脚本之前存在文件。所以我试了一下touch nedb.db,但那不管用。
此外,还有其他奇怪的事情:我有另一个Vue应用程序,它使用NeDB,数据显示。但我没有在应用程序中设置db路径。此应用程序插入的数据不存在于其他应用程序的db文件中。
下面是我的密码。如果有人能帮我。谢谢。
const remote = require('electron').remote;
const app = remote.app;
const path = require('path');
var db = new nedb({
//filename: path.join(app.getPath('userData'), 'library.db'),
filename: './db/nedb.db',
autoload: true
});
let doc = {
dev: true,
message: 'test'
}
db.insert(doc);
db.find({}, function (err, docs) {
console.log(docs)
console.log(err)
})发布于 2018-12-31 17:57:13
NeDB默认为浏览器存储(IndexedDB),如果从renderer进程调用。如果在main进程中创建,它将创建该文件。参见github上的这篇文章。
从岗位:
Nedb让我们使用以下调用创建一个新的自动加载数据存储:
let db = new Datastore({ filename: 'path/to/datafile', autoload: true });然而,这个命令似乎只有在从主进程执行时才是准确的(对于新的电子开发人员来说,这通常是您的main.ts或main.js文件)。 如果从呈现程序进程中执行的类(在BrowserWindow中执行的任何类)执行Datastore创建命令,则nedb将忽略所提供的数据文件,而是在IndexedDB中创建数据库。您将在应用程序的"userData“目录中找到您的数据库(请参阅您的os的文档)如果您真的想让数据库创建和使用在数据存储创建过程中提供的数据库文件,您必须创建和访问该数据文件(添加、删除、.文件)从主要过程。
let collectionsDb: Datastore = new Datastore({ filename: path.join(app.getPath("userData"), "Collections.db"), autoload: true });
const globalAny:any = global;
globalAny.collectionsDb = collectionsDb;
通过调用全局db变量从呈现程序进程访问全局db变量:从“电子”导入{ remote };
private db = remote.getGlobal('collectionDb');
也请参阅OP引用的问题的这个答案。
https://stackoverflow.com/questions/52900791
复制相似问题