orbitdb / field-manual

The Offical User's Guide to OrbitDB
207 stars 43 forks source link

In 01_Tutorial I have some confusion #160

Open Gezi-lzq opened 2 years ago

Gezi-lzq commented 2 years ago

01_Tutorial/02_Managing_Data.md:

const IPFS = require('ipfs')
const cid = await NPP.addNewPiece("QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ")
cid = new IPFS.CID(cid)
const content = await NPP.node.dag.get(cid)
console.log(content.value.payload)

If I use const cid = ..., I should appear when running. TypeError: Assignment to Constant Variable. And, I think it should be in the create () method, when the _init () method is called, addawait this._init();

Below is the main.js file example I have written according to the tutorial in learning. Only by the modifications mentioned above, it can make this file to run normal, I am not sure if the behavior is correct.

const NPP = require("./newpieceplease");
const IPFS = require("ipfs");

async function main() {
  NPP.onready = () => {
    console.log("IPFS node identifier ", NPP.orbitdb.id);
    console.log("database address ", NPP.pieces.id);
  };
  await NPP.create();
  console.log("created");
  // test 
  addPieceTest();
}

async function addPieceTest() {
  let cid = await NPP.addNewPiece(
    "QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ"
  );
  cid = new IPFS.CID(cid);
  const content = await NPP.node.dag.get(cid);
  console.log(content.value.payload);
}
main();

I have modified the AddNewpiece function in order to run, so that I will return to cidatif (existingpiece)

  async addNewPiece(hash, instrument = "Piano") {
    const existingPiece = this.getPieceByHash(hash)
    if (existingPiece) {
-     await this.updatePieceByHash(hash, instrument)
+    const cid = await this.updatePieceByHash(hash, instrument)
+    return cid
    }
    const cid = await this.pieces.put({ hash, instrument })
    return cid;
  }

/01_Tutorial/03_Structuring_Data.md: Utilizing the practice counter ... These can be used in your application code like so:

const piece = NPP.getPieceByHash('QmdzDacgJ9EQF9Z8G3L1fzFwiEu255Nm5WiCey9ntrDPSL')
const cid = await NPP.incrementPracticeCounter(piece)
const content = await NPP.node.dag.get(cid)
console.log(content.value.payload)

const content = await NPP.node.dag.get(cid) Should be changed

const content = await NPP.node.dag.get(new IPFS.CID(cid));