在electronicjs构建中找不到nedb文件

时间:2019-01-26 13:42:45

标签: electron-builder nedb

我正在尝试构建电子应用程序,但是在成功构建应用程序后,未找到nedb文件。

我已经尝试了所有方法,当我尝试使用本地根文件夹中的数据库时,它可以正常工作,但在生产模式下无法使用数据库。

节点版本v10.4.0

import Datastore from 'nedb'
import path from 'path'
import { remote } from 'electron'
const app = remote.app;
const fs = require('fs');

let inventories = path.join(app.getAppPath(), 'app', 'db', 'inventories.db')
let bills = path.join(app.getAppPath(), 'app', 'db', 'bills.db')

var db = {};

let files = [inventories, bills]
files.forEach( (filename) => {
  fs.open(filename,'r',function(err, fd){
    if (err) {
      fs.writeFile(filename, '', function(err) {
          if(err) {
              console.log(err);
          }
          console.log("The file was saved!");
      });
    } else {
      console.log("The file exists!");
    }
  })
})

console.log(files);
db.inventories = new Datastore({
  autoload: true,
  // filename: dbPath + '/inventories.db'
  filename: inventories
});

db.bills = new Datastore({
  autoload: true,
  // filename: dbPath +  '/bills.db'
  filename: bills
});

export default db

我正在收到类似以下错误:

Uncaught Error: ENOENT, app/db/inventories.db not found in /tmp/.mount_mbasevUKaNZD/resources/app.asar
    at notFoundError (ELECTRON_ASAR.js:108)
    at Object.fs.readFile (ELECTRON_ASAR.js:511)
    at /tmp/.mount_mbasevUKaNZD/resources/app.asar/node_modules/nedb/lib/persistence.js:277
    at /tmp/.mount_mbasevUKaNZD/resources/app.asar/node_modules/nedb/lib/storage.js:124
    at fs.js:1283
    at ELECTRON_ASAR.js:115
    at _combinedTickCallback (internal/process/next_tick.js:131)
    at process._tickCallback (internal/process/next_tick.js:180)
notFoundError @ ELECTRON_ASAR.js:108
fs.readFile @ ELECTRON_ASAR.js:511
(anonymous) @ /tmp/.mount_mbasevUKaNZD/resources/app.asar/node_modules/nedb/lib/persistence.js:277
(anonymous) @ /tmp/.mount_mbasevUKaNZD/resources/app.asar/node_modules/nedb/lib/storage.js:124
(anonymous) @ fs.js:1283
(anonymous) @ ELECTRON_ASAR.js:115
_combinedTickCallback @ internal/process/next_tick.js:131
_tickCallback @ internal/process/next_tick.js:180

0 个答案:

没有答案