gpt4 book ai didi

vue.js - NeDB(使用Vue-Electron)无法保存本地数据库文件

转载 作者:行者123 更新时间:2023-12-03 12:26:19 27 4
gpt4 key购买 nike

我正在与NeDB合作开发Vue-Electron。
在使用NeDB时,我遇到了问题,尽管我设置了filenameautoload: true选项,但NeDB不会保存本地文件。

我在加载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 NEDB persistance in Electron app

我认为可能会发生,因为NeDB需要在运行脚本之前存在文件。所以我尝试了 touch nedb.db,但是没有用。

而且,另一个奇怪的事情是:我有另一个正在使用NeDB的Vue应用程序,数据显示了出来。但是我没有在该应用程序中设置数据库路径。该应用程序插入的数据在另一个应用程序的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)
})

最佳答案

如果从renderer进程调用,则NeDB将默认为浏览器存储(IndexedDB)。如果在main进程中创建,它将创建文件。 See this post on github.

从帖子:

Nedb let's you create a new auto-loading Datastore using this call:

let db = new Datastore({ filename: 'path/to/datafile', autoload: true });

It appears, however, that this command is only accurate when performed from the main process (for new Electron developers, this is usually your main.ts or main.js file).

If you perform the Datastore creation command from a class which is executed in the renderer process (any class which is executed in the BrowserWindow), nedb ignores the datafile you provided and creates your database in IndexedDB instead. You'll find your database in your application's "userData" directory (see documentation for your os) If you really want nedb to create and use the database file that you provide during Datastore creation, you must create AND access the data file (add, remove,... documents) from the main process.

  1. Creating the data file from the main process (in main.ts):

let collectionsDb: Datastore = new Datastore({ filename: path.join(app.getPath("userData"), "Collections.db"), autoload: true });

  1. Putting the db variable in a global variable in the main process (in main.ts):

const globalAny:any = global;

globalAny.collectionsDb = collectionsDb;

Accessing the global db variable from the renderer process by calling the global db variable: import { remote } from 'electron';

private db = remote.getGlobal('collectionDb');



另请参阅 this answer,以了解OP所引用的问题。

关于vue.js - NeDB(使用Vue-Electron)无法保存本地数据库文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52900791/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com