Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit ea63d8e

Browse files
tjbdevaphelionz
authored andcommitted
Fixed counter example
If I run this ``` async function main(){ const NPP = require('./newpieceplease'); await NPP.create(); const piece = NPP.getPieceByHash("QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ"); await NPP.getPracticeCount(piece).then(console.log); const cid = await NPP.incrementPracticeCounter(piece); await NPP.getPracticeCount(piece).then(console.log); } main() ``` The first time, the output is: ``` 0 1 ``` But the second time, after the database is already created, the output is ``` 1 1 ``` So, I go into `node` and try some stuff and I find that if you "open" the database with orbitdb.counter(counter.id), then *the value resets*: ``` > const NPP = require('./newpieceplease') undefined > NPP.create() Promise { <pending> } > let counter = NPP.orbitdb.counter("test") undefined > counter = counter.then(a=>counter=a) Promise { <pending> } > let id = counter.id undefined > id '/orbitdb/zdpuAxHhs9pXv7yjEafjvXpLMkUM9dcVVS2JkaS29BYSP3qUE/test' > counter.value 0 > counter.inc() Promise { <pending> } > counter.value 1 > counter = NPP.orbitdb.counter(id) Promise { <pending> } > counter = counter.then(a=>counter=a) Promise { <pending> } > counter.value 0 ``` The manual says: > await this.orbitdb.counter(piece.counter) is a new way of using this.orbitdb.counter, by passing in an existing database address. This will open the existing database instead of creating it I found that if you do the same but load() it seems to work, (adding to the previous node terminal state): ``` > counter.inc() Promise { <pending> } > counter.value 1 > counter = NPP.orbitdb.counter(id) Promise { <pending> } > counter = counter.then(a=>counter=a) Promise { <pending> } > counter.load() Promise { <pending> } > counter.value 1 ``` Notice how after `counter = NPP.orbitdb.counter(id)` and also `counter.load()` the value did not reset to `0` as it did previously, but was retained. Just a problem I noticed, following the tutorial, but if I am wrong please let me know, I'm trying to learn :)
1 parent cc11442 commit ea63d8e

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

01_Tutorial/03_Structuring_Data.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Now, add a few functions to `NewPiecePlease` that utilize the counters when nece
8585

8686
+ async incrementPracticeCounter (piece) {
8787
+ const counter = await this.orbitdb.counter(piece.counter)
88+
+ await counter.load()
8889
+ const cid = await counter.inc()
8990
+ return cid
9091
+ }

0 commit comments

Comments
 (0)