带有lowdb的Angular 6-无法正常使用

时间:2018-11-29 21:57:30

标签: json angular typescript

我正在尝试使用Angular 6创建一个将lowdb用作本地数据库的Electron应用。

对我来说这都是很新的东西,它是反复试验,但我似乎无法弄清楚如何克服以下错误:

我已经使用命令在应用程序中安装了lowdb

npm 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 commentthis 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'

2 个答案:

答案 0 :(得分:2)

首先,如果仅安装了@ 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

  

typicode于2017年8月25日发表评论

     

@DickyKwok我认为这是不可能的。 @typicode你能回答吗?

     

我确认,要使用FileSync或FileAsync适配器保存到磁盘,您需要访问Node 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');
}

答案 1 :(得分:0)

好像您的lowdb在node_modules中丢失了。

尝试

async.timeout.timeout()