lowdb icon indicating copy to clipboard operation
lowdb copied to clipboard

How to read from a JSON file if it requires default data?

Open bobbintb opened this issue 1 year ago • 5 comments

I'm really struggling trying to understand the examples in the readme and elsewhere when trying to read a file. I have a JSON file and I am trying to load and read from. For example:

import { Low } from 'lowdb'
import { JSONFile } from 'lowdb/node'

const adapter = new JSONFile('file.json')
const db = new Low(adapter)
await db.read()
console.log(db.data)

Whatever variation I try, it keeps saying I am missing the default data. The example in the readme to read or create a file includes the default data, which doesn't make any sense to me because it is just an empty object and when I try that, I get an empty database instead of the data from my file. I really don't understand how this is supposed to work.

bobbintb avatar Jun 15 '24 00:06 bobbintb

Default data is a model.

tetuaoro avatar Aug 01 '24 20:08 tetuaoro

removed comment

michielveen avatar Sep 23 '24 13:09 michielveen

I am also facing the same issue not abe to resolve it yet.

intekhab-8674 avatar Feb 20 '25 04:02 intekhab-8674

I am also facing the same issue not abe to resolve it yet.

import { Low } from 'lowdb' import { JSONFile } from 'lowdb/node'

const adapter = new JSONFile('file.json', {}) // add {} and done const db = new Low(adapter) await db.read() console.log(db.data)

mrfzvx12 avatar Mar 11 '25 05:03 mrfzvx12

Just ran into this problem, the Low constructor(in the Low.js file) needs an adapter and default data. So referencing the previous comments' code, this worked for me:

import { Low } from 'lowdb' import { JSONFile } from 'lowdb/node'

const adapter = new JSONFile('file.json') const db = new Low(adapter, {}) // add {} over here await db.read() console.log(db.data)

ngngo0 avatar May 12 '25 08:05 ngngo0