orbitdb / field-manual

The Offical User's Guide to OrbitDB
210 stars 44 forks source link

IPFS Codecs issue #164

Closed elie-h closed 2 years ago

elie-h commented 2 years ago

Hi!

Following the 02_Managing_Data.md section of the tutorial Looks like any calls ipfs with a CID cause a codec issue - I'm using the exact same code in the github from section 2 for define the NewPiecePlease class.

Error:

Projects/orbitdb/node_modules/ipfs-core-utils/cjs/src/multicodecs.js:5
const LOAD_CODEC = codeOrName => Promise.reject(new Error(`No codec found for "${ codeOrName }"`));
                                                ^

Error: No codec found for "undefined"
    at Multicodecs.LOAD_CODEC [as _loadCodec] (/Users/eliehamouche/Projects/orbitdb/node_modules/ipfs-core-utils/cjs/src/multicodecs.js:5:49)
    at Multicodecs.getCodec (/Users/eliehamouche/Projects/orbitdb/node_modules/ipfs-core-utils/cjs/src/multicodecs.js:31:30)
    at get (/Users/eliehamouche/Projects/orbitdb/node_modules/ipfs-core/cjs/src/components/dag/get.js:30:32)
    at DagAPI.get (/Users/eliehamouche/Projects/orbitdb/node_modules/ipfs-core-utils/cjs/src/with-timeout-option.js:18:14)
    at NewPiecePlease.NPP.onready (/Users/eliehamouche/Projects/orbitdb/main.js:13:44)

Node.js v17.3.0

My script:

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

NPP.onready = async () => {
    let cid
    const hash = "QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ";

    cid = await NPP.addNewPiece(hash)
    console.log(cid)

    if (cid) {
        const content = await NPP.node.dag.get(cid)
        console.log(content.value.payload.value)
    }

    console.log('getting all pieces')
    let pieces = NPP.getAllPieces()
    pieces.forEach((piece) => {
        console.log(piece)
    })

    console.log('getting piece by ' + hash)
    piece = NPP.getPieceByHash(hash)
    console.log(piece)

    console.log('getting random piece')
    pieces = NPP.getPieceByInstrument("Piano")
    const randomPiece = pieces[pieces.length * Math.random() | 0]
    console.log(randomPiece)

    cid = await NPP.updatePieceByHash(hash, "Harpsichord")

    if (cid) {
        console.log('update piece' + hash)
        const content = await NPP.node.dag.get(cid)
        console.log(content.value.payload.value)
    }

    cid = await NPP.deletePieceByHash(hash)

    if (cid) {
        const content = await NPP.node.dag.get(cid)
        console.log(content.value.payload)
    }
}

NPP.create()

dependencies:

    "ipfs": "^0.62.1",
    "orbit-db": "^0.28.3"
LucaPanofsky commented 2 years ago

The field manual is outdated in this respect inasmuch as now the dag.get api requires a cid object, see the doc example here.

The cid you are using in your code is the string representation of the cid object, you first have to parse it

let parsedCid = IpfsCore.CID.parse("zdpuAvD7x5ZX5hz96MiEhFpeLayZRbBj75HwNdvJhfz8y3t4T")

before fetching the data

const content = await NPP.node.dag.get(parsedCid)