orbitdb / field-manual

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

TypeError: Cannot read property 'length' of undefined #141

Closed tjbdev closed 3 years ago

tjbdev commented 3 years ago

I am following the field manual , and at this point:

const cid = await NPP.addNewPiece("QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ")
const content = await NPP.node.dag.get(cid)
console.log(content.value.payload)

I get the following error, from entering line 2 into node.js

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

(node:21816) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined at encode (D:\Web Development\OrbitDB_Tutorial\node_modules\multibase\src\rfc4648.js:71:28) at Object.encode (D:\Web Development\OrbitDB_Tutorial\node_modules\multibase\src\rfc4648.js:111:14) at Base.encode (D:\Web Development\OrbitDB_Tutorial\node_modules\multibase\src\base.js:33:23) at toString (D:\Web Development\OrbitDB_Tutorial\node_modules\uint8arrays\to-string.js:45:29) at blockEvent (D:\Web Development\OrbitDB_Tutorial\node_modules\ipfs-bitswap\src\notifications.js:23:38) at Notifications.wantBlock (D:\Web Development\OrbitDB_Tutorial\node_modules\ipfs-bitswap\src\notifications.js:68:22) at Bitswap.get (D:\Web Development\OrbitDB_Tutorial\node_modules\ipfs-bitswap\src\index.js:276:26) at BlockService.get (D:\Web Development\OrbitDB_Tutorial\node_modules\ipfs-block-service\src\index.js:97:28) at IPLDResolver.get (D:\Web Development\OrbitDB_Tutorial\node_modules\ipld\src\index.js:155:33) at get (D:\Web Development\OrbitDB_Tutorial\node_modules\ipfs-core\src\components\dag\get.js:31:25)


I do not know if the following is relevant, but it is an abnormality that I will mention in case.

In chapter 1 of the field manual there is the following:

You will see some activity inside your project's orbitdb/ folder. This is good.

$ ls orbitdb/
QmNrPunxswb2Chmv295GeCvK9FDusWaTr1ZrYhvWV9AtGM/  zdpuB3VvBJHqYCocN4utQrpBseHou88mq2DLh7bUkWviBQSE3/

$ ls orbitdb/zdpuB3VvBJHqYCocN4utQrpBseHou88mq2DLh7bUkWviBQSE3/
pieces/

$ ls orbitdb/zdpuB3VvBJHqYCocN4utQrpBseHou88mq2DLh7bUkWviBQSE3/pieces/
000003.log  CURRENT  LOCK  LOG  MANIFEST-000002

Although I believed that I followed the tutorial perfectly, I do not see a folder of CIDv1 (as in 'zdpu' prefix) in the /orbitdb folder.

However, doing a put( ) (within addNewPiece( )) on the "pieces" docstore that was created does yield a CIDv1.

> const cid = NPP.addNewPiece("QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ")
undefined
> cid
Promise { 'zdpuAmoc6VGLnzpKhjZJGRfxoEUSCkgBSXvCbeyjieqUib7Qu' }

If anyone could help with this issue or try to replicate it, that would be great! Here is my code, in case I've made an error.

newpieceplease.js

class NewPiecePlease {
    constructor(IPFS, OrbitDB){
        Object.assign(this, {IPFS, OrbitDB});
    }

    async create(){
        this.node = await this.IPFS.create({
            preload: {enabled:false},
            repo: "./ipfs",
            EXPIRIMENTAL: {pubsub:true},
            config: {
                Bootstrap: [],
                Addresses: { Swarm: [] }
            }
        })
        await this._init();
    }

    async _init(){
        this.orbitdb = await this.OrbitDB.createInstance(this.node);
        this.defaultOptions = {accessController:{
            write: [this.orbitdb.identity.id]
        }} 

        const docStoreOptions = {
            ...this.defaultOptions,
            indexBy: 'hash'
        }

        this.pieces = await this.orbitdb.docstore('pieces', docStoreOptions);
        await this.pieces.load();
        this.onready();
    }

    async addNewPiece(hash, instrument = "Piano"){
        //const existingPiece = this.getPieceByHash(hash);
        //if(existingPiece){
            //await this.updatePieceByHash(hash, instrument)
            //return;
        //}

        const cid = await this.pieces.put({hash,instrument});
        return cid;

    }

    onready = () => {

    }

}

try{
    const IPFS = require('ipfs');
    const OrbitDB = require('orbit-db');

    module.exports = exports = new NewPiecePlease(IPFS,OrbitDB);

}catch(e){
    window.NPP = new NewPiecePlease(window.IPFS, window.OrbitDB)
}

main.js

async function main(){
    const NPP = require('./newpieceplease');
    await NPP.create();
    const cid = await NPP.addNewPiece("QmNR2n4zywCV61MeMLB6JwPueAPqheqpfiA4fLPMxouEmQ");
    const content = await NPP.node.dag.get(cid);
    console.log(content);
}

main();

Related: orbitdb/orbit-db#839 , though nothing there solved my issue

EDIT: versions "ipfs": "^0.55.3", "orbit-db": "^0.26.1"

tjbdev commented 3 years ago

I checked the readme for orbit-db again and saw

Compatible with js-ipfs versions <= 0.52 and go-ipfs versions <= 0.6.0

So I changed the version of ipfs in package.json to ^0.52 And ran npm install

It didn't get stuck on this error at that point!

bryanguillen commented 2 years ago

@tjbdev , thanks for this thread! Helped me get pass this frustrating issue.

Regards