gpt4 book ai didi

json - 带有 lowdb 的 Angular 6 - 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 17:56:04 33 4
gpt4 key购买 nike

我正在尝试使用 Angular 6 创建一个 Electron 应用程序,该应用程序使用 lowdb作为本地数据库。

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

我已经使用命令在我的应用程序中安装了 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 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

typicode commented on 25 Aug 2017

@DickyKwok I think it's impossible. Can you answer this, @typicode?

I confirm, to save to disk using FileSync or FileAsync adapters you need to have access to 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');
}

关于json - 带有 lowdb 的 Angular 6 - 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53548214/

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