Comments (3)
ok, db.data prints an array with the strings from previous tests I pushed in the DB. And I found the file.json. Its inD:\Users\xxx\AppData\Local\Programs\Microsoft VS Code. Weird.
You can use an absolute path to ensure that file.json is where you expect it to be. You can make it relative to your source file (https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#what-do-i-use-instead-of-__dirname-and-__filename).
So how can it be that the db object has the data that is written in the file.json if I didnt used .read()?
It's not mentioned in the docs, so I understand the confusion. Presets automatically read the database so that can start using data directly.
https://github.com/typicode/lowdb/blob/main/src/presets/node.ts#L29
PS: thank you ๐ but unfortunately I have many questions that are left unanswered because of time/lack of funding ^^' so to be honest here it's a bit of luck ;)
from lowdb.
It works on my side with the following code (I didn't use TS to test it quicker but it shouldn't have an impact):
โฏ rm file.json
โฏ node index.mjs
undefined
โฏ bat file.json
โโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ File: file.json
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1 โ {
2 โ "messages": [
3 โ "HelloWorld"
4 โ ]
5 โ }
โโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โฏ bat index.mjs
โโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ File: index.mjs
โโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
1 โ import { JSONSyncPreset } from "lowdb/node";
2 โ
3 โ const message = 'HelloWorld'
4 โ const defaultData = { messages: [] }
5 โ const db = JSONSyncPreset('file.json', defaultData)
6 โ db.data.messages.push(message)
7 โ
8 โ db.write()
9 โ console.log(db.read());
โโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
/tmp/foo is ๐ฆ v1.0.0 via ๎ v21.2.0
โฏ
So file.json
is created. db.read()
fills db.data
it doesn't return a value. If you want to get log the content of the db, use console.log(db.data)
.
LocalStoragePreset
is if you want to run lowdb in the browser, using window.localStorage
.
I don't know why your code doesn't work though ๐ค
from lowdb.
ok, db.data
prints an array with the strings from previous tests I pushed in the DB. And I found the file.json. Its inD:\Users\xxx\AppData\Local\Programs\Microsoft VS Code
. Weird.
db.read() fills db.data
I am quite confused now. I used the code above without .read()
. I changed console.log(db.read());
to console.log(db.data ());
So how can it be that the db object has the data that is written in the file.json if I didnt used .read()
?
Ps: You are awesome for answering so quickly. Most repos have so many questions on read. Thx <3
from lowdb.
Related Issues (20)
- vue ๅฎ่ฃ lowdbๅ๏ผ็จimport ๆนๅผๅผๅ ฅ็ๆถๅไบง็้ฎ้ข HOT 1
- does lowdb support dot syntax? HOT 4
- error in clustermode HOT 1
- Open to adding support for deno? HOT 7
- Using lowdb in svelte causes 500 error HOT 1
- Writing data doesn't work when using the Lodash Example HOT 3
- Add new key value pair to existing database HOT 1
- Cannot find module 'lowdb/node' or its correspoing type declaractions HOT 4
- Why Json File is pretty HOT 1
- JSONFile path error HOT 2
- db.json updates but db.value() does not show it in terminal HOT 1
- Throws ERR_INVALID_URL HOT 6
- Think again if you don't want to deal with bunch of documents just because of this ESM forcing. HOT 1
- pnpm ๅฎ่ฃ ๅไฝฟ็จๆฅ้ HOT 1
- Lowdb .db.json.tmp version 6.1.1 HOT 6
- .
- error TS1192: Module '"node:fs"' has no default export. HOT 1
- Uncaught TypeError: (0 , import_node_path.dirname) is not a function HOT 1
- Compare the advantages of localForage HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lowdb.