我正在尝试创建一个Angular6的电子应用程序,它使用lowdb作为本地数据库。
这对我来说都是很新的,是反复试验,但我似乎无法找出如何克服以下错误:
我已经用命令在我的应用程序中安装了lowdbnpm install --save lowdb
(编辑:忘了提我已经做过了)
和npm install --save @types/lowdb
我创建了一个服务来与这个“本地数据库”通信。
import { Injectable } from '@angular/core';
import lowdb from 'lowdb';
import { default as FileAsync } from 'lowdb/adapters/FileAsync';
import { CurrentUserModel } from '../models/current-user';
@Injectable({
providedIn: 'root'
})
export class LowdbService {
private db: lowdb.LowdbAsync;
constructor() {
this.initDatabase();
}
set( field: string, value: any ) {
this.db.set( field, value ).write();
}
private async initDatabase() {
const adapter = new FileAsync( 'db.json' );
this.db = await lowdb( adapter );
this.db.defaults( { user: CurrentUserModel } );
}
}
但是,当我将服务包含在组件的构造函数中时,会得到错误。
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb' in '/Users/paul/Projects/application-name/src/app/services'
ERROR in ./src/app/services/lowdb.service.ts
Module not found: Error: Can't resolve 'lowdb/adapters/FileAsync' in '/Users/paul/Projects/application-name/src/app/services'
ℹ 「wdm」: Failed to compile.
ERROR in src/app/services/lowdb.service.ts(2,12): error TS1192: Module '"/Users/paul/Projects/application-name/node_modules/@types/lowdb/index"' has no default export.
src/app/services/lowdb.service.ts(3,14): error TS2305: Module '"/Users/paul/Projects/application-name/node_modules/@types/lowdb/adapters/FileAsync"' has no exported member 'default'.
据我所见,我正在做this Github comment和this Stackoverflow comment中提到的同样的事情。但是我找不到更多的文档。
有人能帮我吗?
更新
使用
import * as lowdb from 'lowdb'
似乎解决了我最初的错误。但是它导致了一些其他的错误。见下文。ERROR in ./node_modules/graceful-fs/polyfills.js
Module not found: Error: Can't resolve 'constants' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/graceful-fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/fs.js
Module not found: Error: Can't resolve 'fs' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
ERROR in ./node_modules/graceful-fs/legacy-streams.js
Module not found: Error: Can't resolve 'stream' in '/Users/paul/Projects/project-name/node_modules/graceful-fs'
最佳答案
首先,如果您只安装了@types/lowdb,这只是该库的类型,那么您需要通过以下方式安装库本身:
npm install --save lowdb
然后,看起来没有默认导出,因此
import lowdb from 'lowdb';
也将不起作用。尝试如下:import { LowdbAsync } from 'lowdb';
import * as lowdb from 'lowdb';
import * as FileAsync from 'lowdb/adapters/FileAsync';
private db: LowdbAsync<any>; // TODO provide correct schema instead of any
private async initDatabase() {
const adapter = new FileAsync('db.json');
this.db = await lowdb(adapter);
this.db.defaults({ user: any });
}
或者你可以试着去掉那些打字机(因为它们可能已经过时了),按照他们的文档所说的做:
https://github.com/typicode/lowdb/tree/master/examples#browser
import low from 'lowdb'
import LocalStorage from 'lowdb/adapters/LocalStorage'
(但是当您查看
@types/lowdb
时,没有这样的默认导出,因此,输入可能已经过时)至于你的其他问题,请看这里:
https://github.com/typicode/lowdb/issues/206#issuecomment-324884867
Typecode于2017年8月25日发表评论
@我认为这是不可能的。你能回答这个问题吗,打字员?
我确认,要使用filesync或fileasync适配器保存到磁盘,您需要具有对节点fs api的访问权限。
看起来您应该改用
LocalStorage
适配器。另一个类似的问题是:How can I use node "fs" in electron within angular 5
您也可以尝试其中一个答案中提出的解决方案:
https://github.com/ThorstenHans/ngx-electron
constructor(private _electronService: ElectronService) {
this.fs = this._electronService.remote.require('fs');
}